class Time

{Time} class monkey-patched with {Timerizer::Duration} support.

Public Class Methods

between(time1, time2) click to toggle source

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
classic_new(*args)
Alias for: new
new(*args) click to toggle source
# 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
Also aliased as: classic_new
since(time) click to toggle source

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
until(time) click to toggle source

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

+(time) click to toggle source
# File lib/timerizer.rb, line 38
def +(time)
  if time.is_a?(Timerizer::Duration)
    time.after(self)
  else
    self.add(time)
  end
end
Also aliased as: add
-(time) click to toggle source
# File lib/timerizer.rb, line 47
def -(time)
  if time.is_a?(Timerizer::Duration)
    time.before(self)
  else
    self.subtract(time)
  end
end
Also aliased as: subtract
add(time)
# else
# end

end

Alias for: +
subtract(time)
Alias for: -
to_date() click to toggle source

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
to_time() click to toggle source

Convert self to {Time}. @see Date#to_time

# File lib/timerizer.rb, line 114
def to_time
  self
end
to_wall() click to toggle source

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