class TimeInterval::Duration
Attributes
days[R]
hours[R]
minutes[R]
months[R]
seconds[R]
weeks[R]
years[R]
Public Class Methods
new(years: 0, months: 0, weeks: 0, days: 0, hours: 0, minutes: 0, seconds: 0)
click to toggle source
# 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
parse(duration_str)
click to toggle source
# 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
Public Instance Methods
add_to(time)
click to toggle source
# File lib/time_interval/duration.rb, line 45 def add_to(time) time + total_seconds end
iso8601()
click to toggle source
# File lib/time_interval/duration.rb, line 49 def iso8601 string = 'P' + iso8601_date string += iso8601_time unless iso8601_time == 'T' string end
present?()
click to toggle source
# File lib/time_interval/duration.rb, line 60 def present? total_seconds > 0 end
subtract_from(time)
click to toggle source
# File lib/time_interval/duration.rb, line 56 def subtract_from(time) time - total_seconds end
total_seconds()
click to toggle source
# 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
Private Instance Methods
iso8601_date()
click to toggle source
# File lib/time_interval/duration.rb, line 71 def iso8601_date string = '' string += "#{years}Y" if years > 0 string += "#{months}M" if months > 0 string += "#{weeks}W" if weeks > 0 string += "#{days}D" if days > 0 string end
iso8601_time()
click to toggle source
# File lib/time_interval/duration.rb, line 81 def iso8601_time string = 'T' string += "#{hours}H" if hours > 0 string += "#{minutes}M" if minutes > 0 string += "#{seconds}S" if seconds > 0 string end