Class: Time

Inherits:
Object
  • Object
show all
Defined in:
lib/timerizer.rb

Overview

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

Defined Under Namespace

Classes: TimeIsInTheFutureError, TimeIsInThePastError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.between(time1, time2) ⇒ Duration

Note:

The two times are interchangable; which comes first doesn’t matter

Calculate the amount of time between two times.

Examples:

Time.between(1.minute.ago, 1.hour.ago)
  => 59.minutes

Parameters:

  • time1 (Time)

    The initial time

  • time2 (Time)

    The final time

Returns:

  • (Duration)

    Calculated time between time1 and time2

See Also:



95
96
97
98
99
# 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_newObject



14
# File 'lib/timerizer.rb', line 14

alias_method :classic_new, :new

.new(*args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# 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

.since(time) ⇒ Duration

Calculates the time since a given time @raise The provided time is in the future

Examples:

Time.since(Time.new(2011, 10, 31))
  => 46 weeks, 5 days, 18 hours, 26 minutes, 10 seconds

Parameters:

  • time (Time)

    The time to calculate since now

Returns:

  • (Duration)

    The time since the provided time

Raises:

See Also:



79
80
81
82
83
# 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) ⇒ Duration

Calculates the time until a given time @raise The provided time is in the past

Examples:

Time.until(Time.new(2012, 12, 25))
  => 13 weeks, 2 days, 6 hours, 31 minutes, 39 seconds

Parameters:

  • time (Time)

    The time until now to calculate

Returns:

  • (Duration)

    The time until the provided time

Raises:

See Also:



64
65
66
67
68
# File 'lib/timerizer.rb', line 64

def self.until(time)
  raise TimeIsInThePastError if Time.now > time.to_time

  Time.between(Time.now, time)
end

Instance Method Details

#+(time) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/timerizer.rb', line 38

def +(time)
  if time.is_a?(Timerizer::Duration)
    time.after(self)
  else
    self.add(time)
  end
end

#-(time) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/timerizer.rb', line 47

def -(time)
  if time.is_a?(Timerizer::Duration)
    time.before(self)
  else
    self.subtract(time)
  end
end

#addObject

# else

# end

end



37
# File 'lib/timerizer.rb', line 37

alias_method :add, :+

#subtractObject



46
# File 'lib/timerizer.rb', line 46

alias_method :subtract, :-

#to_dateDate

Convert Time to Date.

Examples:

Time.new(2000, 1, 1, 12, 30).to_date
  => #<Date: 2000-01-01 ((2451545j,0s,0n),+0s,2299161j)>

Returns:



107
108
109
# File 'lib/timerizer.rb', line 107

def to_date
  Date.new(self.year, self.month, self.day)
end

#to_timeObject

Convert self to Time.

See Also:

  • Date#to_time


114
115
116
# File 'lib/timerizer.rb', line 114

def to_time
  self
end

#to_wallTimerizer::WallClock

Examples:

time = Time.now.to_wall
Date.tomorrow.at(time)
  => 2000-1-2 13:13:27 -0800
  # "Same time tomorrow?"

Returns:



125
126
127
# File 'lib/timerizer.rb', line 125

def to_wall
  Timerizer::WallClock.new(self.hour, self.min, self.sec)
end