class Time
{Time} class monkey-patched with {Timerizer::Duration} support.
Public Class Methods
Calculate the amount of time between two times. @param [Time] time1 The initial time @param [Time] time2 The final time @return [Duration] Calculated time between time1 and time2 @example
Time.between(1.minute.ago, 1.hour.ago) => 59.minutes
@note The two times are interchangable; which comes first doesn't matter @see Time#until @see Time#since
# File lib/timerizer.rb, line 95 def self.between(time1, time2) time_between = (time2.to_time - time1.to_time).abs Timerizer::Duration.new(seconds: time_between.round) end
# File lib/timerizer.rb, line 16 def new(*args) begin Time.classic_new(*args) rescue ArgumentError if args.empty? Time.new else Time.local(*args) end end end
Calculates the time since a given time @param [Time] time The time to calculate since now @return [Duration] The time since the provided time @raise The provided time is in the future @example
Time.since(Time.new(2011, 10, 31)) => 46 weeks, 5 days, 18 hours, 26 minutes, 10 seconds
@see Time#since @see Time#between
# File lib/timerizer.rb, line 79 def self.since(time) raise TimeIsInTheFutureError if time.to_time > Time.now Time.between(Time.now, time) end
Calculates the time until a given time @param [Time] time The time until now to calculate @return [Duration] The time until the provided time @raise The provided time is in the past @example
Time.until(Time.new(2012, 12, 25)) => 13 weeks, 2 days, 6 hours, 31 minutes, 39 seconds
@see Time#since @see Time#between
# File lib/timerizer.rb, line 64 def self.until(time) raise TimeIsInThePastError if Time.now > time.to_time Time.between(Time.now, time) end
Public Instance Methods
# File lib/timerizer.rb, line 38 def +(time) if time.is_a?(Timerizer::Duration) time.after(self) else self.add(time) end end
# File lib/timerizer.rb, line 47 def -(time) if time.is_a?(Timerizer::Duration) time.before(self) else self.subtract(time) end end
Convert {Time} to {Date}. @return [Date] {Time} as {Date} @example
Time.new(2000, 1, 1, 12, 30).to_date => #<Date: 2000-01-01 ((2451545j,0s,0n),+0s,2299161j)>
# File lib/timerizer.rb, line 107 def to_date Date.new(self.year, self.month, self.day) end
Convert self to {Time}. @see Date#to_time
# File lib/timerizer.rb, line 114 def to_time self end
Converts {Time} to {Timerizer::WallClock} @return [Timerizer::WallClock] {Time} as {Timerizer::WallClock} @example
time = Time.now.to_wall Date.tomorrow.at(time) => 2000-1-2 13:13:27 -0800 # "Same time tomorrow?"
# File lib/timerizer.rb, line 125 def to_wall Timerizer::WallClock.new(self.hour, self.min, self.sec) end