Class: ISO8601::Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/iso8601/duration.rb

Overview

A duration representation. When no base is provided, all atoms use an average factor which affects the result of any computation like `#to_seconds`.

Examples:

d = ISO8601::Duration.new('P2Y1MT2H')
d.years  # => #<ISO8601::Years:0x000000051adee8 @atom=2.0, @base=nil>
d.months # => #<ISO8601::Months:0x00000004f230b0 @atom=1.0, @base=nil>
d.days   # => #<ISO8601::Days:0x00000005205468 @atom=0, @base=nil>
d.hours  # => #<ISO8601::Hours:0x000000051e02a8 @atom=2.0, @base=nil>
d.to_seconds # => 65707200.0

Explicit base date time

base = ISO8601::DateTime.new('2014-08017')
d = ISO8601::Duration.new('P2Y1MT2H', base)
d.years  # => #<ISO8601::Years:0x000000051adee8 @atom=2.0,
                @base=#<ISO8601::DateTime...>>
d.months # => #<ISO8601::Months:0x00000004f230b0 @atom=1.0,
                @base=#<ISO8601::DateTime...>>
d.days   # => #<ISO8601::Days:0x00000005205468 @atom=0,
                @base=#<ISO8601::DateTime...>>
d.hours  # => #<ISO8601::Hours:0x000000051e02a8 @atom=2.0,
                @base=#<ISO8601::DateTime...>>
d.to_seconds # => 65757600.0

Number of seconds versus patterns

di = ISO8601::Duration.new(65707200)
dp = ISO8601::Duration.new('P2Y1MT2H')
ds = ISO8601::Duration.new('P65707200S')
di == dp # => true
di == ds # => true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, base = nil) ⇒ Duration

Returns a new instance of Duration.

Parameters:

  • input (String, Numeric)

    The duration pattern

  • base (ISO8601::DateTime, nil) (defaults to: nil)

    (nil) The base datetime to calculate the duration against an specific point in time.


42
43
44
45
46
47
# File 'lib/iso8601/duration.rb', line 42

def initialize(input, base = nil)
  @original = input
  @pattern = to_pattern
  @atoms = atomize(@pattern)
  @base = validate_base(base)
end

Instance Attribute Details

#atomsHash<Float> (readonly)

Raw atoms result of parsing the given pattern.

Returns:

  • (Hash<Float>)

53
54
55
# File 'lib/iso8601/duration.rb', line 53

def atoms
  @atoms
end

#baseISO8601::DateTime?

Datetime base.

Returns:


59
60
61
# File 'lib/iso8601/duration.rb', line 59

def base
  @base
end

#patternString (readonly) Also known as: to_s

Returns The string representation of the duration.

Returns:

  • (String)

    The string representation of the duration


72
73
74
# File 'lib/iso8601/duration.rb', line 72

def pattern
  @pattern
end

#signInteger (readonly)

The Integer representation of the duration sign.

Returns:

  • (Integer)

123
124
125
# File 'lib/iso8601/duration.rb', line 123

def sign
  @sign
end

Instance Method Details

#+(other) ⇒ ISO8601::Duration

Addition

Parameters:

Returns:

Raises:


138
139
140
# File 'lib/iso8601/duration.rb', line 138

def +(other)
  seconds_to_iso(to_seconds + fetch_seconds(other))
end

#-(other) ⇒ ISO8601::Duration

Substraction

Parameters:

Returns:

Raises:


149
150
151
# File 'lib/iso8601/duration.rb', line 149

def -(other)
  seconds_to_iso(to_seconds - fetch_seconds(other))
end

#==(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)

Raises:


158
159
160
# File 'lib/iso8601/duration.rb', line 158

def ==(other)
  (to_seconds == fetch_seconds(other))
end

#absISO8601::Duration

Returns The absolute representation of the duration.

Returns:


127
128
129
# File 'lib/iso8601/duration.rb', line 127

def abs
  self.class.new(pattern.sub(/^[-+]/, ''), base)
end

#daysISO8601::Days

Returns The days of the duration.

Returns:


97
98
99
# File 'lib/iso8601/duration.rb', line 97

def days
  ISO8601::Days.new(atoms[:days], base)
end

#eql?(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)

166
167
168
# File 'lib/iso8601/duration.rb', line 166

def eql?(other)
  (hash == other.hash)
end

#hashFixnum

Returns:

  • (Fixnum)

172
173
174
# File 'lib/iso8601/duration.rb', line 172

def hash
  [atoms.values, self.class].hash
end

#hoursISO8601::Hours

Returns The hours of the duration.

Returns:


103
104
105
# File 'lib/iso8601/duration.rb', line 103

def hours
  ISO8601::Hours.new(atoms[:hours], base)
end

#minutesISO8601::Minutes

Returns The minutes of the duration.

Returns:


109
110
111
# File 'lib/iso8601/duration.rb', line 109

def minutes
  ISO8601::Minutes.new(atoms[:minutes], base)
end

#monthsISO8601::Months

Returns The months of the duration.

Returns:


83
84
85
86
87
# File 'lib/iso8601/duration.rb', line 83

def months  # Changes the base to compute the months for the right base year

  month_base = base.nil? ? nil : base + years.to_seconds
  ISO8601::Months.new(atoms[:months], month_base)
end

#secondsISO8601::Seconds

Returns The seconds of the duration.

Returns:


115
116
117
# File 'lib/iso8601/duration.rb', line 115

def seconds
  ISO8601::Seconds.new(atoms[:seconds], base)
end

#to_daysNumeric

Returns The duration in days.

Returns:

  • (Numeric)

    The duration in days


193
194
195
# File 'lib/iso8601/duration.rb', line 193

def to_days
  (to_seconds / 86400)
end

#to_fFloat

Returns The duration in seconds coerced to float.

Returns:

  • (Float)

    The duration in seconds coerced to float


205
206
207
# File 'lib/iso8601/duration.rb', line 205

def to_f
  to_seconds.to_f
end

#to_iInteger

Returns The integer part of the duration in seconds.

Returns:

  • (Integer)

    The integer part of the duration in seconds


199
200
201
# File 'lib/iso8601/duration.rb', line 199

def to_i
  to_seconds.to_i
end

#to_patternString

Converts original input into a valid ISO 8601 duration pattern.

Returns:

  • (String)

180
181
182
# File 'lib/iso8601/duration.rb', line 180

def to_pattern
  (@original.is_a? Numeric) ? "PT#{@original}S" : @original
end

#to_secondsNumeric

Returns The duration in seconds.

Returns:

  • (Numeric)

    The duration in seconds


186
187
188
189
# File 'lib/iso8601/duration.rb', line 186

def to_seconds
  atoms = [years, months, weeks, days, hours, minutes, seconds]
  atoms.map(&:to_seconds).reduce(&:+)
end

#weeksISO8601::Weeks

Returns The weeks of the duration.

Returns:


91
92
93
# File 'lib/iso8601/duration.rb', line 91

def weeks
  ISO8601::Weeks.new(atoms[:weeks], base)
end

#yearsISO8601::Years

Returns The years of the duration.

Returns:


77
78
79
# File 'lib/iso8601/duration.rb', line 77

def years
  ISO8601::Years.new(atoms[:years], base)
end