Class: ISO8601::DateTime

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

Overview

A DateTime representation

Examples:

dt = DateTime.new('2014-05-28T19:53Z')
dt.year #=> 2014

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

Instance Method Summary collapse

Constructor Details

#initialize(date_time) ⇒ DateTime

Returns a new instance of DateTime.

Parameters:

  • date_time (String)

    The datetime pattern



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

#secondObject (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

Parameters:

  • seconds (Numeric)

    The seconds to add



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

Parameters:

  • seconds (Numeric)

    The seconds to substract



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_aObject

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_sObject

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