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



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

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>)


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

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



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

def pattern
  @pattern
end

#signInteger (readonly)

The Integer representation of the duration sign.

Returns:

  • (Integer)


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

def sign
  @sign
end

Instance Method Details

#+(other) ⇒ ISO8601::Duration

Addition

Parameters:

Returns:



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

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

#-(other) ⇒ ISO8601::Duration

Substraction

Parameters:

Returns:



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

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

#==(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


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

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

#absISO8601::Duration

Returns The absolute representation of the duration.

Returns:



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

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

#daysISO8601::Days

Returns The days of the duration.

Returns:



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

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

#eql?(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


133
134
135
# File 'lib/iso8601/duration.rb', line 133

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

#hashFixnum

Returns:

  • (Fixnum)


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

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

#hoursISO8601::Hours

Returns The hours of the duration.

Returns:



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

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

#minutesISO8601::Minutes

Returns The minutes of the duration.

Returns:



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

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

#monthsISO8601::Months

Returns The months of the duration.

Returns:



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

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

#secondsISO8601::Seconds

Returns The seconds of the duration.

Returns:



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

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

#to_pattern(original) ⇒ String

Converts original input into a valid ISO 8601 duration pattern.

Returns:

  • (String)


147
148
149
# File 'lib/iso8601/duration.rb', line 147

def to_pattern(original)
  (original.is_a? Numeric) ? "PT#{original}S" : original
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



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

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:



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

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

#yearsISO8601::Years

Returns The years of the duration.

Returns:



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

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