Class: ISO8601::DateTime
- Inherits:
-
Object
- Object
- ISO8601::DateTime
- Extended by:
- Forwardable
- Defined in:
- lib/iso8601/date_time.rb
Overview
A DateTime representation
Instance Attribute Summary collapse
-
#second ⇒ Object
readonly
Returns the value of attribute second.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Addition.
-
#-(other) ⇒ Object
Substraction.
- #==(other) ⇒ Boolean
- #eql?(other) ⇒ Boolean
- #hash ⇒ Fixnum
-
#initialize(date_time) ⇒ DateTime
constructor
A new instance of DateTime.
-
#to_a ⇒ Object
(also: #atoms)
Converts DateTime to an array of atoms.
-
#to_f ⇒ Object
Converts DateTime to a floating point number of seconds since the Epoch.
-
#to_s ⇒ Object
Converts DateTime to a formated string.
Constructor Details
#initialize(date_time) ⇒ DateTime
Returns a new instance of DateTime.
23 24 25 26 27 |
# File 'lib/iso8601/date_time.rb', line 23 def initialize(date_time) @original = date_time @date_time = parse(date_time) @second = @date_time.second + @date_time.second_fraction.to_f.round(1) end |
Instance Attribute Details
#second ⇒ Object (readonly)
Returns the value of attribute second.
19 20 21 |
# File 'lib/iso8601/date_time.rb', line 19 def second @second end |
Instance Method Details
#+(other) ⇒ Object
Addition
32 33 34 35 36 |
# File 'lib/iso8601/date_time.rb', line 32 def +(other) moment = @date_time.to_time.localtime(zone) + other.round(1) self.class.new(moment.strftime('%Y-%m-%dT%H:%M:%S.%N%:z')) end |
#-(other) ⇒ Object
Substraction
41 42 43 44 45 |
# File 'lib/iso8601/date_time.rb', line 41 def -(other) moment = @date_time.to_time.localtime(zone) - other.round(1) self.class.new(moment.strftime('%Y-%m-%dT%H:%M:%S.%N%:z')) end |
#==(other) ⇒ Boolean
68 69 70 |
# File 'lib/iso8601/date_time.rb', line 68 def ==(other) (hash == other.hash) end |
#eql?(other) ⇒ Boolean
75 76 77 |
# File 'lib/iso8601/date_time.rb', line 75 def eql?(other) (hash == other.hash) end |
#hash ⇒ Fixnum
80 81 82 |
# File 'lib/iso8601/date_time.rb', line 80 def hash [to_f, self.class].hash end |
#to_a ⇒ Object Also known as: atoms
Converts DateTime to an array of atoms.
55 56 57 |
# File 'lib/iso8601/date_time.rb', line 55 def to_a [year, month, day, hour, minute, second, zone] end |
#to_f ⇒ Object
Converts DateTime to a floating point number of seconds since the Epoch.
61 62 63 |
# File 'lib/iso8601/date_time.rb', line 61 def to_f to_time.to_f end |
#to_s ⇒ Object
Converts DateTime to a formated string
48 49 50 51 52 |
# File 'lib/iso8601/date_time.rb', line 48 def to_s second_format = (second % 1).zero? ? '%02d' % second : '%04.1f' % second "%04d-%02d-%02dT%02d:%02d:#{second_format}#{zone}" % atoms end |