Class: TimeInterval::Duration
- Inherits:
-
Object
- Object
- TimeInterval::Duration
- Defined in:
- lib/time_interval/duration.rb
Instance Attribute Summary collapse
-
#days ⇒ Object
readonly
Returns the value of attribute days.
-
#hours ⇒ Object
readonly
Returns the value of attribute hours.
-
#minutes ⇒ Object
readonly
Returns the value of attribute minutes.
-
#months ⇒ Object
readonly
Returns the value of attribute months.
-
#seconds ⇒ Object
readonly
Returns the value of attribute seconds.
-
#weeks ⇒ Object
readonly
Returns the value of attribute weeks.
-
#years ⇒ Object
readonly
Returns the value of attribute years.
Class Method Summary collapse
Instance Method Summary collapse
- #add_to(time) ⇒ Object
-
#initialize(years: 0, months: 0, weeks: 0, days: 0, hours: 0, minutes: 0, seconds: 0) ⇒ Duration
constructor
A new instance of Duration.
- #iso8601 ⇒ Object
- #present? ⇒ Boolean
- #subtract_from(time) ⇒ Object
- #total_seconds ⇒ Object
Constructor Details
#initialize(years: 0, months: 0, weeks: 0, days: 0, hours: 0, minutes: 0, seconds: 0) ⇒ Duration
Returns a new instance of Duration.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/time_interval/duration.rb', line 34 def initialize(years: 0, months: 0, weeks: 0, days: 0, hours: 0, minutes: 0, seconds: 0) @years = years @months = months @weeks = weeks @days = days @hours = hours @minutes = minutes @seconds = seconds end |
Instance Attribute Details
#days ⇒ Object (readonly)
Returns the value of attribute days.
32 33 34 |
# File 'lib/time_interval/duration.rb', line 32 def days @days end |
#hours ⇒ Object (readonly)
Returns the value of attribute hours.
32 33 34 |
# File 'lib/time_interval/duration.rb', line 32 def hours @hours end |
#minutes ⇒ Object (readonly)
Returns the value of attribute minutes.
32 33 34 |
# File 'lib/time_interval/duration.rb', line 32 def minutes @minutes end |
#months ⇒ Object (readonly)
Returns the value of attribute months.
32 33 34 |
# File 'lib/time_interval/duration.rb', line 32 def months @months end |
#seconds ⇒ Object (readonly)
Returns the value of attribute seconds.
32 33 34 |
# File 'lib/time_interval/duration.rb', line 32 def seconds @seconds end |
#weeks ⇒ Object (readonly)
Returns the value of attribute weeks.
32 33 34 |
# File 'lib/time_interval/duration.rb', line 32 def weeks @weeks end |
#years ⇒ Object (readonly)
Returns the value of attribute years.
32 33 34 |
# File 'lib/time_interval/duration.rb', line 32 def years @years end |
Class Method Details
.parse(duration_str) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/time_interval/duration.rb', line 3 def self.parse(duration_str) if duration_str['PT'] date_str = nil time_str = duration_str else date_str, time_str = duration_str.split('T') end if date_str y = date_str['Y'] ? date_str.match(/(\d+)Y/)[1].to_i : 0 m = date_str['M'] ? date_str.match(/(\d+)M/)[1].to_i : 0 w = date_str['W'] ? date_str.match(/(\d+)W/)[1].to_i : 0 d = date_str['D'] ? date_str.match(/(\d+)D/)[1].to_i : 0 else y = m = w = d = 0 end if time_str h = time_str['H'] ? time_str.match(/(\d+)H/)[1].to_i : 0 mi = time_str['M'] ? time_str.match(/(\d+)M/)[1].to_i : 0 s = time_str['S'] ? time_str.match(/(\d+)S/)[1].to_i : 0 else h = mi = s = 0 end new(years: y, months: m, weeks: w, days: d, hours: h, minutes: mi, seconds: s) end |
Instance Method Details
#add_to(time) ⇒ Object
45 46 47 |
# File 'lib/time_interval/duration.rb', line 45 def add_to(time) time + total_seconds end |
#iso8601 ⇒ Object
49 50 51 52 53 54 |
# File 'lib/time_interval/duration.rb', line 49 def iso8601 string = 'P' + iso8601_date string += iso8601_time unless iso8601_time == 'T' string end |
#present? ⇒ Boolean
60 61 62 |
# File 'lib/time_interval/duration.rb', line 60 def present? total_seconds > 0 end |
#subtract_from(time) ⇒ Object
56 57 58 |
# File 'lib/time_interval/duration.rb', line 56 def subtract_from(time) time - total_seconds end |
#total_seconds ⇒ Object
64 65 66 67 |
# File 'lib/time_interval/duration.rb', line 64 def total_seconds years.years + months.months + weeks.weeks + days.days + hours.hours + minutes.minutes + seconds.seconds end |