Class: Time
- Inherits:
-
Object
- Object
- Time
- Defined in:
- lib/timerizer.rb
Overview
Time class monkey-patched with Timerizer::Duration support.
Defined Under Namespace
Classes: TimeIsInTheFutureError, TimeIsInThePastError
Class Method Summary collapse
-
.between(time1, time2) ⇒ Duration
Calculate the amount of time between two times.
- .classic_new ⇒ Object
- .new(*args) ⇒ Object
-
.since(time) ⇒ Duration
Calculates the time since a given time @raise The provided time is in the future.
-
.until(time) ⇒ Duration
Calculates the time until a given time @raise The provided time is in the past.
Instance Method Summary collapse
- #+(time) ⇒ Object
- #-(time) ⇒ Object
-
#add ⇒ Object
# else # end end.
- #subtract ⇒ Object
- #to_date ⇒ Date
-
#to_time ⇒ Object
Convert self to Time.
-
#to_wall ⇒ Timerizer::WallClock
Converts Time to Timerizer::WallClock.
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.
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_new ⇒ Object
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 |
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 |
#add ⇒ Object
# else
# end
end
37 |
# File 'lib/timerizer.rb', line 37 alias_method :add, :+ |
#subtract ⇒ Object
46 |
# File 'lib/timerizer.rb', line 46 alias_method :subtract, :- |
#to_date ⇒ Date
107 108 109 |
# File 'lib/timerizer.rb', line 107 def to_date Date.new(self.year, self.month, self.day) end |
#to_time ⇒ Object
Convert self to Time.
114 115 116 |
# File 'lib/timerizer.rb', line 114 def to_time self end |
#to_wall ⇒ Timerizer::WallClock
Converts Time to Timerizer::WallClock
125 126 127 |
# File 'lib/timerizer.rb', line 125 def to_wall Timerizer::WallClock.new(self.hour, self.min, self.sec) end |