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
- #+(seconds) ⇒ Object
- #-(other) ⇒ Object
- #<=>(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.
- #inspect ⇒ Object
- #minute ⇒ Object
- #month ⇒ Object
- #second ⇒ Object
- #to_a ⇒ Object
- #to_date ⇒ Object
- #to_h ⇒ Object
- #to_i ⇒ Object
- #to_iso8601 ⇒ Object
- #to_rfc3339 ⇒ 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.
17 18 19 20 21 |
# File 'lib/archaeo/timestamp.rb', line 17 def initialize(year:, month: 1, day: 1, hour: 0, minute: 0, second: 0) @to_time = Time.utc(year, month, day, hour, minute, second) freeze end |
Instance Attribute Details
#to_time ⇒ Object (readonly)
Returns the value of attribute to_time.
15 16 17 |
# File 'lib/archaeo/timestamp.rb', line 15 def to_time @to_time end |
Class Method Details
.coerce(value) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/archaeo/timestamp.rb', line 53 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
43 44 45 46 47 |
# File 'lib/archaeo/timestamp.rb', line 43 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
49 50 51 |
# File 'lib/archaeo/timestamp.rb', line 49 def self.now from_time(Time.now) end |
.parse(string) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/archaeo/timestamp.rb', line 23 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
#+(seconds) ⇒ Object
84 85 86 |
# File 'lib/archaeo/timestamp.rb', line 84 def +(seconds) self.class.from_time(@to_time + seconds) end |
#-(other) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/archaeo/timestamp.rb', line 88 def -(other) if other.is_a?(self.class) @to_time - other.to_time else self.class.from_time(@to_time - other) end end |
#<=>(other) ⇒ Object
96 97 98 99 100 |
# File 'lib/archaeo/timestamp.rb', line 96 def <=>(other) return nil unless other.is_a?(self.class) to_s <=> other.to_s end |
#day ⇒ Object
118 119 120 |
# File 'lib/archaeo/timestamp.rb', line 118 def day @to_time.day end |
#eql?(other) ⇒ Boolean
106 107 108 |
# File 'lib/archaeo/timestamp.rb', line 106 def eql?(other) self == other end |
#hash ⇒ Object
102 103 104 |
# File 'lib/archaeo/timestamp.rb', line 102 def hash to_s.hash end |
#hour ⇒ Object
122 123 124 |
# File 'lib/archaeo/timestamp.rb', line 122 def hour @to_time.hour end |
#inspect ⇒ Object
143 144 145 |
# File 'lib/archaeo/timestamp.rb', line 143 def inspect "#<#{self.class.name} #{self}>" end |
#minute ⇒ Object
126 127 128 |
# File 'lib/archaeo/timestamp.rb', line 126 def minute @to_time.min end |
#month ⇒ Object
114 115 116 |
# File 'lib/archaeo/timestamp.rb', line 114 def month @to_time.month end |
#second ⇒ Object
130 131 132 |
# File 'lib/archaeo/timestamp.rb', line 130 def second @to_time.sec end |
#to_a ⇒ Object
139 140 141 |
# File 'lib/archaeo/timestamp.rb', line 139 def to_a [year, month, day, hour, minute, second] end |
#to_date ⇒ Object
68 69 70 |
# File 'lib/archaeo/timestamp.rb', line 68 def to_date Date.new(year, month, day) end |
#to_h ⇒ Object
134 135 136 137 |
# File 'lib/archaeo/timestamp.rb', line 134 def to_h { year: year, month: month, day: day, hour: hour, minute: minute, second: second } end |
#to_i ⇒ Object
72 73 74 |
# File 'lib/archaeo/timestamp.rb', line 72 def to_i @to_time.to_i end |
#to_iso8601 ⇒ Object
76 77 78 |
# File 'lib/archaeo/timestamp.rb', line 76 def to_iso8601 @to_time.iso8601 end |
#to_rfc3339 ⇒ Object
80 81 82 |
# File 'lib/archaeo/timestamp.rb', line 80 def to_rfc3339 @to_time.rfc3339 end |
#to_s ⇒ Object
64 65 66 |
# File 'lib/archaeo/timestamp.rb', line 64 def to_s @to_time.strftime(FORMAT) end |
#year ⇒ Object
110 111 112 |
# File 'lib/archaeo/timestamp.rb', line 110 def year @to_time.year end |