Class: Time
- Inherits:
-
Object
- Object
- Time
- Defined in:
- lib/timerizer.rb
Overview
Time class monkeywrenched with RelativeTime support.
Defined Under Namespace
Classes: TimeIsInTheFutureError, TimeIsInThePastError
Class Method Summary collapse
-
.between(time1, time2) ⇒ RelativeTime
Calculate the amount of time between two times.
- .classic_new ⇒ Object
- .new(*args) ⇒ Object
-
.since(time) ⇒ RelativeTime
Calculates the time since a given time @raise The provided time is in the future.
-
.until(time) ⇒ RelativeTime
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 ⇒ WallClock
Class Method Details
.between(time1, time2) ⇒ RelativeTime
Note:
The two times are interchangable; which comes first doesn’t matter
Calculate the amount of time between two times.
709 710 711 712 713 |
# File 'lib/timerizer.rb', line 709 def self.between(time1, time2) time_between = (time2.to_time - time1.to_time).abs RelativeTime.new(time_between.round) end |
.classic_new ⇒ Object
628 |
# File 'lib/timerizer.rb', line 628 alias_method :classic_new, :new |
.new(*args) ⇒ Object
630 631 632 633 634 635 636 637 638 639 640 |
# File 'lib/timerizer.rb', line 630 def new(*args) begin Time.classic_new(*args) rescue ArgumentError if args.empty? Time.new else Time.local(*args) end end end |
.since(time) ⇒ RelativeTime
Calculates the time since a given time @raise The provided time is in the future
693 694 695 696 697 |
# File 'lib/timerizer.rb', line 693 def self.since(time) raise TimeIsInTheFutureError if time.to_time > Time.now Time.between(Time.now, time) end |
.until(time) ⇒ RelativeTime
Calculates the time until a given time @raise The provided time is in the past
678 679 680 681 682 |
# File 'lib/timerizer.rb', line 678 def self.until(time) raise TimeIsInThePastError if Time.now > time.to_time Time.between(Time.now, time) end |
Instance Method Details
#+(time) ⇒ Object
652 653 654 655 656 657 658 |
# File 'lib/timerizer.rb', line 652 def +(time) if time.is_a? RelativeTime time.after(self) else self.add(time) end end |
#-(time) ⇒ Object
661 662 663 664 665 666 667 |
# File 'lib/timerizer.rb', line 661 def -(time) if time.is_a? RelativeTime time.before(self) else self.subtract(time) end end |
#add ⇒ Object
# else
# end
end
651 |
# File 'lib/timerizer.rb', line 651 alias_method :add, :+ |
#subtract ⇒ Object
660 |
# File 'lib/timerizer.rb', line 660 alias_method :subtract, :- |
#to_date ⇒ Date
720 721 722 |
# File 'lib/timerizer.rb', line 720 def to_date Date.new(self.year, self.month, self.day) end |
#to_time ⇒ Object
Convert self to Time.
726 727 728 |
# File 'lib/timerizer.rb', line 726 def to_time self end |