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
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_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



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

.nowObject



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

#dayObject



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

def day
  @to_time.day
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?(other)
  self == other
end

#hashObject



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

def hash
  to_s.hash
end

#hourObject



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

def hour
  @to_time.hour
end

#inspectObject



143
144
145
# File 'lib/archaeo/timestamp.rb', line 143

def inspect
  "#<#{self.class.name} #{self}>"
end

#minuteObject



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

def minute
  @to_time.min
end

#monthObject



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

def month
  @to_time.month
end

#secondObject



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

def second
  @to_time.sec
end

#to_aObject



139
140
141
# File 'lib/archaeo/timestamp.rb', line 139

def to_a
  [year, month, day, hour, minute, second]
end

#to_dateObject



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

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

#to_hObject



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_iObject



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

def to_i
  @to_time.to_i
end

#to_iso8601Object



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

def to_iso8601
  @to_time.iso8601
end

#to_rfc3339Object



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

def to_rfc3339
  @to_time.rfc3339
end

#to_sObject



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

def to_s
  @to_time.strftime(FORMAT)
end

#yearObject



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

def year
  @to_time.year
end