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 to compute the amount of seconds.

Examples:

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

Explicit base date time

base = ISO8601::DateTime.new('2014-08017')
d.to_seconds(base) # => 65757600.0

Number of seconds versus patterns

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Duration

Returns a new instance of Duration.

Parameters:

  • input (String, Numeric)

    The duration pattern



29
30
31
32
33
# File 'lib/iso8601/duration.rb', line 29

def initialize(input)
  @original = input
  @pattern = to_pattern(input)
  @atoms = atomize(@pattern)
end

Instance Attribute Details

#atomsHash<Float> (readonly)

Raw atoms result of parsing the given pattern.

Returns:

  • (Hash<Float>)


39
40
41
# File 'lib/iso8601/duration.rb', line 39

def atoms
  @atoms
end

#patternString (readonly) Also known as: to_s

Returns The string representation of the duration.

Returns:

  • (String)

    The string representation of the duration



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

def pattern
  @pattern
end

#signInteger (readonly)

The Integer representation of the duration sign.

Returns:

  • (Integer)


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

def sign
  @sign
end

Instance Method Details

#+(other) ⇒ ISO8601::Duration

Addition

Parameters:

Returns:



106
107
108
# File 'lib/iso8601/duration.rb', line 106

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

#-(other) ⇒ ISO8601::Duration

Substraction

Parameters:

Returns:



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

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

#-@ISO8601::Duration

Returns:



131
132
133
# File 'lib/iso8601/duration.rb', line 131

def -@
  seconds_to_iso(-to_seconds)
end

#==(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


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

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

#absISO8601::Duration

Returns The absolute representation of the duration.

Returns:



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

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

#daysISO8601::Days

Returns The days of the duration.

Returns:



66
67
68
# File 'lib/iso8601/duration.rb', line 66

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

#eql?(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


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

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

#hashFixnum

Returns:

  • (Fixnum)


145
146
147
# File 'lib/iso8601/duration.rb', line 145

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

#hoursISO8601::Hours

Returns The hours of the duration.

Returns:



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

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

#minutesISO8601::Minutes

Returns The minutes of the duration.

Returns:



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

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

#monthsISO8601::Months

Returns The months of the duration.

Returns:



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

def months
  ISO8601::Months.new(atoms[:months])
end

#secondsISO8601::Seconds

Returns The seconds of the duration.

Returns:



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

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

#to_pattern(original) ⇒ String

Converts original input into a valid ISO 8601 duration pattern.

Returns:

  • (String)


153
154
155
156
157
158
159
# File 'lib/iso8601/duration.rb', line 153

def to_pattern(original)
  if original.is_a? Numeric
    "#{original < 0 ? '-' : ''}PT#{original.abs}S"
  else
    original
  end
end

#to_seconds(base = nil) ⇒ Numeric

Returns The duration in seconds.

Parameters:

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

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

Returns:

  • (Numeric)

    The duration in seconds



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

def to_seconds(base = nil)
  rest = [weeks, days, hours, minutes, seconds].map(&:to_seconds)

  years.to_seconds(base) + months_to_seconds(base) + rest.reduce(&:+)
end

#weeksISO8601::Weeks

Returns The weeks of the duration.

Returns:



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

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

#yearsISO8601::Years

Returns The years of the duration.

Returns:



48
49
50
# File 'lib/iso8601/duration.rb', line 48

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