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.



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_timeObject (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

.nowObject



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

#dayObject



87
88
89
# File 'lib/archaeo/timestamp.rb', line 87

def day
  @to_time.day
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?(other)
  self == other
end

#hashObject



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

def hash
  to_s.hash
end

#hourObject



91
92
93
# File 'lib/archaeo/timestamp.rb', line 91

def hour
  @to_time.hour
end

#minuteObject



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

def minute
  @to_time.min
end

#monthObject



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

def month
  @to_time.month
end

#secondObject



99
100
101
# File 'lib/archaeo/timestamp.rb', line 99

def second
  @to_time.sec
end

#to_sObject



61
62
63
# File 'lib/archaeo/timestamp.rb', line 61

def to_s
  @to_time.strftime(FORMAT)
end

#yearObject



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

def year
  @to_time.year
end