Class: ISO8601::Duration
- Inherits:
-
Object
- Object
- ISO8601::Duration
- Defined in:
- lib/iso8601/duration.rb
Overview
Support fraction values for years, months, days, weeks, hours and minutes
Represents a duration in ISO 8601 format
Instance Attribute Summary collapse
-
#atoms ⇒ Object
readonly
Returns the value of attribute atoms.
-
#base ⇒ Object
Returns the value of attribute base.
Instance Method Summary collapse
-
#+(duration) ⇒ ISO8601::Duration
Addition.
-
#-(duration) ⇒ ISO8601::Duration
Substraction.
- #==(duration) ⇒ Boolean
-
#abs ⇒ ISO8601::Duration
The absolute representation of the duration.
-
#days ⇒ ISO8601::Days
The days of the duration.
-
#hours ⇒ ISO8601::Hours
The hours of the duration.
-
#initialize(pattern, 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_s ⇒ String
The string representation of the duration.
-
#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(pattern, base = nil) ⇒ Duration
Returns a new instance of Duration.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/iso8601/duration.rb', line 15 def initialize(pattern, base = nil) # we got seconds instead of an ISO8601 duration pattern = "PT#{pattern}S" if (pattern.kind_of? Numeric) @duration = /^(\+|-)? # Sign P( ( (\d+Y)? # Years (\d+M)? # Months (\d+D)? # Days (T (\d+H)? # Hours (\d+M)? # Minutes (\d+(?:\.\d+)?S)? # Seconds )? # Time ) |(\d+W) # Weeks ) # Duration $/x.match(pattern) or raise ISO8601::Errors::UnknownPattern.new(pattern) @base = base valid_pattern? valid_base? @atoms = { :years => @duration[4].nil? ? 0 : @duration[4].chop.to_f * sign, :months => @duration[5].nil? ? 0 : @duration[5].chop.to_f * sign, :weeks => @duration[11].nil? ? 0 : @duration[11].chop.to_f * sign, :days => @duration[6].nil? ? 0 : @duration[6].chop.to_f * sign, :hours => @duration[8].nil? ? 0 : @duration[8].chop.to_f * sign, :minutes => @duration[9].nil? ? 0 : @duration[9].chop.to_f * sign, :seconds => @duration[10].nil? ? 0 : @duration[10].chop.to_f * sign } end |
Instance Attribute Details
#atoms ⇒ Object (readonly)
Returns the value of attribute atoms.
10 11 12 |
# File 'lib/iso8601/duration.rb', line 10 def atoms @atoms end |
#base ⇒ Object
Returns the value of attribute base.
10 11 12 |
# File 'lib/iso8601/duration.rb', line 10 def base @base end |
Instance Method Details
#+(duration) ⇒ ISO8601::Duration
Addition
117 118 119 120 121 122 |
# File 'lib/iso8601/duration.rb', line 117 def +(duration) raise ISO8601::Errors::DurationBaseError.new(duration) if @base.to_s != duration.base.to_s d1 = to_seconds d2 = duration.to_seconds return seconds_to_iso(d1 + d2) end |
#-(duration) ⇒ ISO8601::Duration
Substraction
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/iso8601/duration.rb', line 130 def -(duration) raise ISO8601::Errors::DurationBaseError.new(duration) if @base.to_s != duration.base.to_s d1 = to_seconds d2 = duration.to_seconds duration = d1 - d2 if duration == 0 return ISO8601::Duration.new('PT0S') else return seconds_to_iso(duration) end end |
#==(duration) ⇒ Boolean
146 147 148 149 |
# File 'lib/iso8601/duration.rb', line 146 def ==(duration) raise ISO8601::Errors::DurationBaseError.new(duration) if @base.to_s != duration.base.to_s (self.to_seconds == duration.to_seconds) end |
#abs ⇒ ISO8601::Duration
Returns The absolute representation of the duration.
106 107 108 109 |
# File 'lib/iso8601/duration.rb', line 106 def abs absolute = self.to_s.sub(/^[-+]/, '') return ISO8601::Duration.new(absolute) end |
#days ⇒ ISO8601::Days
Returns The days of the duration.
80 81 82 |
# File 'lib/iso8601/duration.rb', line 80 def days ISO8601::Days.new(@atoms[:days], @base) end |
#hours ⇒ ISO8601::Hours
Returns The hours of the duration.
85 86 87 |
# File 'lib/iso8601/duration.rb', line 85 def hours ISO8601::Hours.new(@atoms[:hours], @base) end |
#minutes ⇒ ISO8601::Minutes
Returns The minutes of the duration.
90 91 92 |
# File 'lib/iso8601/duration.rb', line 90 def minutes ISO8601::Minutes.new(@atoms[:minutes], @base) end |
#months ⇒ ISO8601::Months
Returns The months of the duration.
68 69 70 71 72 |
# File 'lib/iso8601/duration.rb', line 68 def months # Changes the base to compute the months for the right base year base = @base.nil? ? nil : @base + self.years.to_seconds ISO8601::Months.new(@atoms[:months], base) end |
#seconds ⇒ ISO8601::Seconds
Returns The seconds of the duration.
95 96 97 |
# File 'lib/iso8601/duration.rb', line 95 def seconds ISO8601::Seconds.new(@atoms[:seconds], @base) end |
#to_s ⇒ String
Returns The string representation of the duration.
58 59 60 |
# File 'lib/iso8601/duration.rb', line 58 def to_s @duration[0] end |
#to_seconds ⇒ Numeric
Returns The duration in seconds.
100 101 102 103 |
# File 'lib/iso8601/duration.rb', line 100 def to_seconds years, months, weeks, days, hours, minutes, seconds = self.years.to_seconds, self.months.to_seconds, self.weeks.to_seconds, self.days.to_seconds, self.hours.to_seconds, self.minutes.to_seconds, self.seconds.to_seconds return years + months + weeks + days + hours + minutes + seconds end |
#weeks ⇒ ISO8601::Weeks
Returns The weeks of the duration.
75 76 77 |
# File 'lib/iso8601/duration.rb', line 75 def weeks ISO8601::Weeks.new(@atoms[:weeks], @base) end |
#years ⇒ ISO8601::Years
Returns The years of the duration.
63 64 65 |
# File 'lib/iso8601/duration.rb', line 63 def years ISO8601::Years.new(@atoms[:years], @base) end |