Class: ISO8601::Time
- Inherits:
-
Object
- Object
- ISO8601::Time
- Extended by:
- Forwardable
- Defined in:
- lib/iso8601/time.rb
Overview
A Time representation
Constant Summary collapse
- FORMAT =
'T%H:%M:%S%:z'
- FORMAT_WITH_FRACTION =
'T%H:%M:%S.%2N%:z'
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
-
#+(seconds) ⇒ ISO8601::Time
Forwards the time the given amount of seconds.
-
#-(seconds) ⇒ ISO8601::Time
Backwards the date the given amount of seconds.
-
#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.
34 35 36 37 38 39 40 |
# File 'lib/iso8601/time.rb', line 34 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 end |
Instance Attribute Details
#atoms ⇒ Object (readonly)
The original atoms
26 27 28 |
# File 'lib/iso8601/time.rb', line 26 def atoms @atoms end |
#second ⇒ Object (readonly)
The second atom
23 24 25 |
# File 'lib/iso8601/time.rb', line 23 def second @second end |
#separator ⇒ Object (readonly)
The separator used in the original ISO 8601 string.
20 21 22 |
# File 'lib/iso8601/time.rb', line 20 def separator @separator end |
Instance Method Details
#+(seconds) ⇒ ISO8601::Time
Forwards the time the given amount of seconds.
47 48 49 50 51 52 |
# File 'lib/iso8601/time.rb', line 47 def +(seconds) moment = @time.to_time.localtime(zone) + seconds format = moment.subsec.zero? ? FORMAT : FORMAT_WITH_FRACTION ISO8601::Time.new(moment.strftime(format), ::Date.parse(moment.strftime('%Y-%m-%d'))) end |
#-(seconds) ⇒ ISO8601::Time
Backwards the date the given amount of seconds.
59 60 61 62 63 64 |
# File 'lib/iso8601/time.rb', line 59 def -(seconds) moment = @time.to_time.localtime(zone) - seconds format = moment.subsec.zero? ? FORMAT : FORMAT_WITH_FRACTION ISO8601::Time.new(moment.strftime(format), ::Date.parse(moment.strftime('%Y-%m-%d'))) end |
#to_a ⇒ Object
Converts self to an array of atoms.
73 74 75 |
# File 'lib/iso8601/time.rb', line 73 def to_a [hour, minute, second, zone] end |
#to_s ⇒ Object
Converts self to a time component representation.
67 68 69 70 |
# File 'lib/iso8601/time.rb', line 67 def to_s format = @time.second_fraction.zero? ? FORMAT : FORMAT_WITH_FRACTION @time.strftime(format) end |