Class: ISO8601::Time
- Inherits:
-
Object
- Object
- ISO8601::Time
- Extended by:
- Forwardable
- Defined in:
- lib/iso8601/time.rb
Overview
A Time representation
Instance Attribute Summary collapse
-
#atoms ⇒ Object
readonly
The original atoms.
-
#second ⇒ Object
readonly
The second atom.
-
#separator ⇒ Object
readonly
The separator used in the original ISO 8601 string.
Instance Method Summary collapse
-
#+(other) ⇒ ISO8601::Time
Forwards the time the given amount of seconds.
-
#-(other) ⇒ ISO8601::Time
Backwards the date the given amount of seconds.
- #==(other) ⇒ Boolean
- #eql?(other) ⇒ Boolean
- #hash ⇒ Fixnum
-
#initialize(input, base = ::Date.today) ⇒ Time
constructor
A new instance of Time.
-
#to_a ⇒ Object
Converts self to an array of atoms.
-
#to_s ⇒ Object
Converts self to a time component representation.
Constructor Details
#initialize(input, base = ::Date.today) ⇒ Time
Returns a new instance of Time.
38 39 40 41 42 43 44 |
# File 'lib/iso8601/time.rb', line 38 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
#atoms ⇒ Object (readonly)
The original atoms
33 34 35 |
# File 'lib/iso8601/time.rb', line 33 def atoms @atoms end |
#second ⇒ Object (readonly)
The second atom
29 30 31 |
# File 'lib/iso8601/time.rb', line 29 def second @second end |
#separator ⇒ Object (readonly)
The separator used in the original ISO 8601 string.
25 26 27 |
# File 'lib/iso8601/time.rb', line 25 def separator @separator end |
Instance Method Details
#+(other) ⇒ ISO8601::Time
Forwards the time the given amount of seconds.
74 75 76 77 78 79 |
# File 'lib/iso8601/time.rb', line 74 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.
87 88 89 90 91 92 |
# File 'lib/iso8601/time.rb', line 87 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
50 51 52 |
# File 'lib/iso8601/time.rb', line 50 def ==(other) (hash == other.hash) end |
#eql?(other) ⇒ Boolean
58 59 60 |
# File 'lib/iso8601/time.rb', line 58 def eql?(other) (hash == other.hash) end |
#hash ⇒ Fixnum
64 65 66 |
# File 'lib/iso8601/time.rb', line 64 def hash [atoms, self.class].hash end |
#to_a ⇒ Object
Converts self to an array of atoms.
104 105 106 |
# File 'lib/iso8601/time.rb', line 104 def to_a [hour, minute, second, zone] end |
#to_s ⇒ Object
Converts self to a time component representation.
96 97 98 99 100 |
# File 'lib/iso8601/time.rb', line 96 def to_s second_format = format((second % 1).zero? ? '%02d' : '%04.1f', second) format("T%02d:%02d:#{second_format}#{zone}", *atoms) end |