Class: Archaeo::Timestamp

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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
# 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)
end

Instance Attribute Details

#to_timeObject (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



52
53
54
55
56
57
58
59
60
61
# File 'lib/archaeo/timestamp.rb', line 52

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



42
43
44
45
46
# File 'lib/archaeo/timestamp.rb', line 42

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

.nowObject



48
49
50
# File 'lib/archaeo/timestamp.rb', line 48

def self.now
  from_time(Time.now)
end

.parse(string) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/archaeo/timestamp.rb', line 22

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



83
84
85
# File 'lib/archaeo/timestamp.rb', line 83

def +(seconds)
  self.class.from_time(@to_time + seconds)
end

#-(other) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/archaeo/timestamp.rb', line 87

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



95
96
97
98
99
# File 'lib/archaeo/timestamp.rb', line 95

def <=>(other)
  return nil unless other.is_a?(self.class)

  to_s <=> other.to_s
end

#dayObject



117
118
119
# File 'lib/archaeo/timestamp.rb', line 117

def day
  @to_time.day
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/archaeo/timestamp.rb', line 105

def eql?(other)
  self == other
end

#hashObject



101
102
103
# File 'lib/archaeo/timestamp.rb', line 101

def hash
  to_s.hash
end

#hourObject



121
122
123
# File 'lib/archaeo/timestamp.rb', line 121

def hour
  @to_time.hour
end

#minuteObject



125
126
127
# File 'lib/archaeo/timestamp.rb', line 125

def minute
  @to_time.min
end

#monthObject



113
114
115
# File 'lib/archaeo/timestamp.rb', line 113

def month
  @to_time.month
end

#secondObject



129
130
131
# File 'lib/archaeo/timestamp.rb', line 129

def second
  @to_time.sec
end

#to_dateObject



67
68
69
# File 'lib/archaeo/timestamp.rb', line 67

def to_date
  Date.new(year, month, day)
end

#to_iObject



71
72
73
# File 'lib/archaeo/timestamp.rb', line 71

def to_i
  @to_time.to_i
end

#to_iso8601Object



75
76
77
# File 'lib/archaeo/timestamp.rb', line 75

def to_iso8601
  @to_time.iso8601
end

#to_rfc3339Object



79
80
81
# File 'lib/archaeo/timestamp.rb', line 79

def to_rfc3339
  @to_time.rfc3339
end

#to_sObject



63
64
65
# File 'lib/archaeo/timestamp.rb', line 63

def to_s
  @to_time.strftime(FORMAT)
end

#yearObject



109
110
111
# File 'lib/archaeo/timestamp.rb', line 109

def year
  @to_time.year
end