Class: ISO8601::DateTime
- Inherits:
-
Object
- Object
- ISO8601::DateTime
- Extended by:
- Forwardable
- Defined in:
- lib/iso8601/dateTime.rb
Overview
A DateTime representation
Constant Summary collapse
- FORMAT =
'%Y-%m-%dT%H:%M:%S%:z'
- FORMAT_WITH_FRACTION =
'%Y-%m-%dT%H:%M:%S.%2N%:z'
Instance Attribute Summary collapse
-
#second ⇒ Object
readonly
Returns the value of attribute second.
Instance Method Summary collapse
-
#+(seconds) ⇒ Object
Addition.
-
#-(seconds) ⇒ Object
Substraction.
-
#initialize(date_time) ⇒ DateTime
constructor
A new instance of DateTime.
-
#to_a ⇒ Object
Converts DateTime to an array of atoms.
-
#to_s ⇒ Object
Converts DateTime to a formated string.
Constructor Details
#initialize(date_time) ⇒ DateTime
Returns a new instance of DateTime.
24 25 26 27 28 |
# File 'lib/iso8601/dateTime.rb', line 24 def initialize(date_time) @original = date_time @date_time = parse(date_time) @second = @date_time.second + @date_time.second_fraction.to_f end |
Instance Attribute Details
#second ⇒ Object (readonly)
Returns the value of attribute second.
17 18 19 |
# File 'lib/iso8601/dateTime.rb', line 17 def second @second end |
Instance Method Details
#+(seconds) ⇒ Object
Addition
33 34 35 36 37 38 |
# File 'lib/iso8601/dateTime.rb', line 33 def +(seconds) moment = @date_time.to_time.localtime(zone) + seconds format = moment.subsec.zero? ? FORMAT : FORMAT_WITH_FRACTION ISO8601::DateTime.new(moment.strftime(format)) end |
#-(seconds) ⇒ Object
Substraction
43 44 45 46 47 48 |
# File 'lib/iso8601/dateTime.rb', line 43 def -(seconds) moment = @date_time.to_time.localtime(zone) - seconds format = moment.subsec.zero? ? FORMAT : FORMAT_WITH_FRACTION ISO8601::DateTime.new(moment.strftime(format)) end |
#to_a ⇒ Object
Converts DateTime to an array of atoms.
57 58 59 |
# File 'lib/iso8601/dateTime.rb', line 57 def to_a [year, month, day, hour, minute, second, zone] end |
#to_s ⇒ Object
Converts DateTime to a formated string
51 52 53 54 |
# File 'lib/iso8601/dateTime.rb', line 51 def to_s format = @date_time.second_fraction.zero? ? FORMAT : FORMAT_WITH_FRACTION @date_time.strftime(format) end |