Class: Archaeo::Timestamp
- Inherits:
-
Object
- Object
- Archaeo::Timestamp
- Includes:
- Comparable
- Defined in:
- lib/archaeo/timestamp.rb
Overview
Value object representing a Wayback Machine timestamp (YYYYMMDDHHmmss).
Supports parsing, formatting, comparison, and coercion from various time representations.
Constant Summary collapse
- FORMAT =
"%Y%m%d%H%M%S"
Instance Attribute Summary collapse
-
#to_time ⇒ Object
readonly
Returns the value of attribute to_time.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #day ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
- #hour ⇒ Object
-
#initialize(year:, month: 1, day: 1, hour: 0, minute: 0, second: 0) ⇒ Timestamp
constructor
A new instance of Timestamp.
- #minute ⇒ Object
- #month ⇒ Object
- #second ⇒ Object
- #to_s ⇒ Object
- #year ⇒ Object
Constructor Details
#initialize(year:, month: 1, day: 1, hour: 0, minute: 0, second: 0) ⇒ Timestamp
Returns a new instance of Timestamp.
15 16 17 18 |
# File 'lib/archaeo/timestamp.rb', line 15 def initialize(year:, month: 1, day: 1, hour: 0, minute: 0, second: 0) @to_time = Time.utc(year, month, day, hour, minute, second) end |
Instance Attribute Details
#to_time ⇒ Object (readonly)
Returns the value of attribute to_time.
13 14 15 |
# File 'lib/archaeo/timestamp.rb', line 13 def to_time @to_time end |
Class Method Details
.coerce(value) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/archaeo/timestamp.rb', line 50 def self.coerce(value) case value when Timestamp then value when String then parse(value) when Time then from_time(value) else raise ArgumentError, "Cannot coerce #{value.class} to Archaeo::Timestamp" end end |
.from_time(time) ⇒ Object
40 41 42 43 44 |
# File 'lib/archaeo/timestamp.rb', line 40 def self.from_time(time) utc = time.getutc new(year: utc.year, month: utc.month, day: utc.day, hour: utc.hour, minute: utc.min, second: utc.sec) end |
.now ⇒ Object
46 47 48 |
# File 'lib/archaeo/timestamp.rb', line 46 def self.now from_time(Time.now) end |
.parse(string) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/archaeo/timestamp.rb', line 20 def self.parse(string) year = string[0, 4].to_i month = string[4, 2].to_i if string.length >= 6 day = string[6, 2].to_i if string.length >= 8 new(year: year, month: month, day: day, **parse_time_parts(string)) end |
Instance Method Details
#<=>(other) ⇒ Object
65 66 67 68 69 |
# File 'lib/archaeo/timestamp.rb', line 65 def <=>(other) return nil unless other.is_a?(self.class) to_s <=> other.to_s end |
#day ⇒ Object
87 88 89 |
# File 'lib/archaeo/timestamp.rb', line 87 def day @to_time.day end |
#eql?(other) ⇒ Boolean
75 76 77 |
# File 'lib/archaeo/timestamp.rb', line 75 def eql?(other) self == other end |
#hash ⇒ Object
71 72 73 |
# File 'lib/archaeo/timestamp.rb', line 71 def hash to_s.hash end |
#hour ⇒ Object
91 92 93 |
# File 'lib/archaeo/timestamp.rb', line 91 def hour @to_time.hour end |
#minute ⇒ Object
95 96 97 |
# File 'lib/archaeo/timestamp.rb', line 95 def minute @to_time.min end |
#month ⇒ Object
83 84 85 |
# File 'lib/archaeo/timestamp.rb', line 83 def month @to_time.month end |
#second ⇒ Object
99 100 101 |
# File 'lib/archaeo/timestamp.rb', line 99 def second @to_time.sec end |
#to_s ⇒ Object
61 62 63 |
# File 'lib/archaeo/timestamp.rb', line 61 def to_s @to_time.strftime(FORMAT) end |
#year ⇒ Object
79 80 81 |
# File 'lib/archaeo/timestamp.rb', line 79 def year @to_time.year end |