Class: ISO8601::Time

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/iso8601/time.rb

Overview

A Time representation

Examples:

t = Time.new('10:11:12')
t = Time.new('T10:11:12.5Z')
t.hour # => 10
t.minute # => 11
t.second # => 12.5
t.zone # => '+00:00'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, base = ::Date.today) ⇒ Time

Returns a new instance of Time.

Parameters:

  • input (String)

    The time pattern

  • base (Date) (defaults to: ::Date.today)

    The base date to determine the time


36
37
38
39
40
41
42
# File 'lib/iso8601/time.rb', line 36

def initialize(input, base = ::Date.today)
  @original = input
  @base = base
  @atoms = atomize(input)
  @time = compose(@atoms, @base)
  @second = @time.second + @time.second_fraction.to_f.round(1)
end

Instance Attribute Details

#atomsObject (readonly)

The original atoms


31
32
33
# File 'lib/iso8601/time.rb', line 31

def atoms
  @atoms
end

#secondObject (readonly)

The second atom


27
28
29
# File 'lib/iso8601/time.rb', line 27

def second
  @second
end

#separatorObject (readonly)

The separator used in the original ISO 8601 string.


23
24
25
# File 'lib/iso8601/time.rb', line 23

def separator
  @separator
end

Instance Method Details

#+(other) ⇒ ISO8601::Time

Forwards the time the given amount of seconds.

Parameters:

  • other (Numeric)

    The seconds to add

Returns:


72
73
74
75
76
77
# File 'lib/iso8601/time.rb', line 72

def +(other)
  moment = @time.to_time.localtime(zone) + other.to_f.round(1)
  base = ::Date.parse(moment.strftime('%Y-%m-%d'))

  self.class.new(moment.strftime('T%H:%M:%S.%L%:z'), base)
end

#-(other) ⇒ ISO8601::Time

Backwards the date the given amount of seconds.

Parameters:

  • other (Numeric)

    The seconds to remove

Returns:


85
86
87
88
89
90
# File 'lib/iso8601/time.rb', line 85

def -(other)
  moment = @time.to_time.localtime(zone) - other.to_f.round(1)
  base = ::Date.parse(moment.strftime('%Y-%m-%d'))

  self.class.new(moment.strftime('T%H:%M:%S.%L%:z'), base)
end

#==(other) ⇒ Boolean

Parameters:

  • other (#hash)

    The contrast to compare against

Returns:

  • (Boolean)

48
49
50
# File 'lib/iso8601/time.rb', line 48

def ==(other)
  (hash == other.hash)
end

#eql?(other) ⇒ Boolean

Parameters:

  • other (#hash)

    The contrast to compare against

Returns:

  • (Boolean)

56
57
58
# File 'lib/iso8601/time.rb', line 56

def eql?(other)
  (hash == other.hash)
end

#hashFixnum

Returns:

  • (Fixnum)

62
63
64
# File 'lib/iso8601/time.rb', line 62

def hash
  [atoms, self.class].hash
end

#to_aObject

Converts self to an array of atoms.


102
103
104
# File 'lib/iso8601/time.rb', line 102

def to_a
  [hour, minute, second, zone]
end

#to_sObject

Converts self to a time component representation.


94
95
96
97
98
# File 'lib/iso8601/time.rb', line 94

def to_s
  second_format = format((second % 1).zero? ? '%02d' : '%04.1f', second)

  format("T%02d:%02d:#{second_format}#{zone}", *atoms)
end