Class: Crono::TimeOfDay

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/crono/time_of_day.rb

Overview

TimeOfDay describes a certain hour and minute (on any day)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hour, min) ⇒ TimeOfDay

Returns a new instance of TimeOfDay.



20
21
22
# File 'lib/crono/time_of_day.rb', line 20

def initialize(hour, min)
  @hour, @min = hour, min
end

Instance Attribute Details

#hourObject

Returns the value of attribute hour.



6
7
8
# File 'lib/crono/time_of_day.rb', line 6

def hour
  @hour
end

#minObject

Returns the value of attribute min.



6
7
8
# File 'lib/crono/time_of_day.rb', line 6

def min
  @min
end

Class Method Details

.parse(value) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/crono/time_of_day.rb', line 8

def self.parse(value)
  time =
    case value
    when String then Time.parse(value).utc
    when Hash   then Time.now.change(value).utc
    when Time   then value.utc
    else
      fail "Unknown TimeOfDay format: #{value.inspect}"
    end
  new time.hour, time.min
end

Instance Method Details

#<=>(other) ⇒ Object



32
33
34
# File 'lib/crono/time_of_day.rb', line 32

def <=>(other)
  to_i <=> other.to_i
end

#to_iObject



24
25
26
# File 'lib/crono/time_of_day.rb', line 24

def to_i
  @hour * 60 + @min
end

#to_sObject



28
29
30
# File 'lib/crono/time_of_day.rb', line 28

def to_s
  '%02d:%02d' % [@hour, @min]
end