Class: ISO8601::Duration
- Inherits:
-
Object
- Object
- ISO8601::Duration
- 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`.
Instance Attribute Summary collapse
-
#atoms ⇒ Hash<Float>
readonly
Raw atoms result of parsing the given pattern.
-
#base ⇒ ISO8601::DateTime?
Datetime base.
-
#pattern ⇒ String
(also: #to_s)
readonly
The string representation of the duration.
-
#sign ⇒ Integer
readonly
The Integer representation of the duration sign.
Instance Method Summary collapse
-
#+(other) ⇒ ISO8601::Duration
Addition.
-
#-(other) ⇒ ISO8601::Duration
Substraction.
- #==(other) ⇒ Boolean
-
#abs ⇒ ISO8601::Duration
The absolute representation of the duration.
-
#days ⇒ ISO8601::Days
The days of the duration.
- #eql?(other) ⇒ Boolean
- #hash ⇒ Fixnum
-
#hours ⇒ ISO8601::Hours
The hours of the duration.
-
#initialize(input, base = nil) ⇒ Duration
constructor
A new instance of Duration.
-
#minutes ⇒ ISO8601::Minutes
The minutes of the duration.
-
#months ⇒ ISO8601::Months
The months of the duration.
-
#seconds ⇒ ISO8601::Seconds
The seconds of the duration.
-
#to_days ⇒ Numeric
The duration in days.
-
#to_f ⇒ Float
The duration in seconds coerced to float.
-
#to_i ⇒ Integer
The integer part of the duration in seconds.
-
#to_pattern ⇒ String
Converts original input into a valid ISO 8601 duration pattern.
-
#to_seconds ⇒ Numeric
The duration in seconds.
-
#weeks ⇒ ISO8601::Weeks
The weeks of the duration.
-
#years ⇒ ISO8601::Years
The years of the duration.
Constructor Details
#initialize(input, base = nil) ⇒ Duration
Returns a new instance of Duration.
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
#atoms ⇒ Hash<Float> (readonly)
Raw atoms result of parsing the given pattern.
52 53 54 |
# File 'lib/iso8601/duration.rb', line 52 def atoms @atoms end |
#base ⇒ ISO8601::DateTime?
Datetime base.
57 58 59 |
# File 'lib/iso8601/duration.rb', line 57 def base @base end |
#pattern ⇒ String (readonly) Also known as: to_s
Returns The string representation of the duration.
68 69 70 |
# File 'lib/iso8601/duration.rb', line 68 def pattern @pattern end |
#sign ⇒ Integer (readonly)
The Integer representation of the duration sign.
111 112 113 |
# File 'lib/iso8601/duration.rb', line 111 def sign @sign end |
Instance Method Details
#+(other) ⇒ ISO8601::Duration
Addition
124 125 126 127 |
# File 'lib/iso8601/duration.rb', line 124 def +(other) compare_bases(other) seconds_to_iso(to_seconds + other.to_seconds) end |
#-(other) ⇒ ISO8601::Duration
Substraction
135 136 137 138 |
# File 'lib/iso8601/duration.rb', line 135 def -(other) compare_bases(other) seconds_to_iso(to_seconds - other.to_seconds) end |
#==(other) ⇒ Boolean
144 145 146 147 |
# File 'lib/iso8601/duration.rb', line 144 def ==(other) compare_bases(other) (to_seconds == other.to_seconds) end |
#abs ⇒ ISO8601::Duration
Returns The absolute representation of the duration.
114 115 116 |
# File 'lib/iso8601/duration.rb', line 114 def abs self.class.new(pattern.sub(/^[-+]/, ''), base) end |
#days ⇒ ISO8601::Days
Returns The days of the duration.
89 90 91 |
# File 'lib/iso8601/duration.rb', line 89 def days ISO8601::Days.new(atoms[:days], base) end |
#eql?(other) ⇒ Boolean
152 153 154 |
# File 'lib/iso8601/duration.rb', line 152 def eql?(other) (hash == other.hash) end |
#hash ⇒ Fixnum
157 158 159 |
# File 'lib/iso8601/duration.rb', line 157 def hash [atoms.values, self.class].hash end |
#hours ⇒ ISO8601::Hours
Returns The hours of the duration.
94 95 96 |
# File 'lib/iso8601/duration.rb', line 94 def hours ISO8601::Hours.new(atoms[:hours], base) end |
#minutes ⇒ ISO8601::Minutes
Returns The minutes of the duration.
99 100 101 |
# File 'lib/iso8601/duration.rb', line 99 def minutes ISO8601::Minutes.new(atoms[:minutes], base) end |
#months ⇒ ISO8601::Months
Returns The months of the duration.
77 78 79 80 81 |
# File 'lib/iso8601/duration.rb', line 77 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 |
#seconds ⇒ ISO8601::Seconds
Returns The seconds of the duration.
104 105 106 |
# File 'lib/iso8601/duration.rb', line 104 def seconds ISO8601::Seconds.new(atoms[:seconds], base) end |
#to_days ⇒ Numeric
Returns The duration in days.
175 176 177 |
# File 'lib/iso8601/duration.rb', line 175 def to_days (to_seconds / 86400) end |
#to_f ⇒ Float
Returns The duration in seconds coerced to float.
185 186 187 |
# File 'lib/iso8601/duration.rb', line 185 def to_f to_seconds.to_f end |
#to_i ⇒ Integer
Returns The integer part of the duration in seconds.
180 181 182 |
# File 'lib/iso8601/duration.rb', line 180 def to_i to_seconds.to_i end |
#to_pattern ⇒ String
Converts original input into a valid ISO 8601 duration pattern.
164 165 166 |
# File 'lib/iso8601/duration.rb', line 164 def to_pattern (@original.is_a? Numeric) ? "PT#{@original}S" : @original end |
#to_seconds ⇒ Numeric
Returns The duration in seconds.
169 170 171 172 |
# File 'lib/iso8601/duration.rb', line 169 def to_seconds atoms = [years, months, weeks, days, hours, minutes, seconds] atoms.map(&:to_seconds).reduce(&:+) end |
#weeks ⇒ ISO8601::Weeks
Returns The weeks of the duration.
84 85 86 |
# File 'lib/iso8601/duration.rb', line 84 def weeks ISO8601::Weeks.new(atoms[:weeks], base) end |
#years ⇒ ISO8601::Years
Returns The years of the duration.
72 73 74 |
# File 'lib/iso8601/duration.rb', line 72 def years ISO8601::Years.new(atoms[:years], base) end |