Class: Syctask::Times
- Inherits:
-
Object
- Object
- Syctask::Times
- Defined in:
- lib/syctask/times.rb
Overview
Times class represents a time consisting of hour and minutes
Instance Attribute Summary collapse
-
#h ⇒ Object
readonly
Hour of the Times object.
-
#m ⇒ Object
readonly
Minute of the Times object.
Instance Method Summary collapse
-
#diff(time = Time.now) ⇒ Object
Calculates the difference between this time and the provided time.
-
#initialize(time) ⇒ Times
constructor
Creates a new Times object.
-
#round_up ⇒ Object
Rounds the time to the next hour if minutes is greater than 0.
-
#time ⇒ Object
Returns a Time object with the current date and the hour and minute of this Times object.
Constructor Details
#initialize(time) ⇒ Times
Creates a new Times object. time has to be provided as an array with the content as [“hour”,“minute”] e.g. [“10”,“15”] is 15 past 10
13 14 15 16 |
# File 'lib/syctask/times.rb', line 13 def initialize(time) @h = time[0].to_i @m = time[1].to_i end |
Instance Attribute Details
#h ⇒ Object (readonly)
Hour of the Times object
7 8 9 |
# File 'lib/syctask/times.rb', line 7 def h @h end |
#m ⇒ Object (readonly)
Minute of the Times object
9 10 11 |
# File 'lib/syctask/times.rb', line 9 def m @m end |
Instance Method Details
#diff(time = Time.now) ⇒ Object
Calculates the difference between this time and the provided time. If no time is given the current time is used.
Example:
This time = 9:35
New time = 10:20
diff(time) = 0:45
Will return [hour,min] in the example [0,45]
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/syctask/times.rb', line 37 def diff(time = Time.now) diff_minutes = (time.hour - @h) * 60 + (time.min - @m) signum = diff_minutes == 0 ? 0 : diff_minutes / diff_minutes.abs diff_h = diff_minutes.abs / 60 diff_m = diff_minutes.abs % 60 if signum < 0 if diff_h > 0 [signum * diff_h, diff_m] else [diff_h, signum * diff_m] end else [diff_h, diff_m] end end |
#round_up ⇒ Object
Rounds the time to the next hour if minutes is greater than 0
19 20 21 |
# File 'lib/syctask/times.rb', line 19 def round_up @m > 0 ? @h+1 : @h end |
#time ⇒ Object
Returns a Time object with the current date and the hour and minute of this Times object
25 26 27 28 |
# File 'lib/syctask/times.rb', line 25 def time now = Time.now Time.local(now.year,now.mon,now.day,@h,@m,0) end |