Class: Gadgeto::TimeOfDay

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

Overview

A TimeOfDay represents the time of day.

Limitation: 24 hours only.

Defined Under Namespace

Classes: InvalidTimeOfDayFormat, TimeOfDayOutOfRange

Constant Summary collapse

ONE_HOUR_IN_MINUTES =

:nodoc:

60
HOURS_ONE_DAY =

:nodoc:

24

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time_of_day) ⇒ TimeOfDay

Creates a new TimeOfDay object.

TimeOfDay.new("08:00") #=> 08:00
TimeOfDay.new("x8:00") #=> raises InvalidTimeOfDayFormat


23
24
25
26
27
28
29
# File 'lib/gadgeto/time_of_day.rb', line 23

def initialize(time_of_day)
  match = /^([0-2]?\d):([0-5]\d)$/.match(time_of_day)
  raise InvalidTimeOfDayFormat if match.nil?
  @hour = match[1].to_i
  @minute = match[2].to_i
  raise InvalidTimeOfDayFormat if (@hour > HOURS_ONE_DAY) || (@hour == HOURS_ONE_DAY && @minute > 0)
end

Instance Attribute Details

#hourObject (readonly)

Returns the hour component.

t = TimeOfDay.new("08:00") #=> 08:00
t.hour                     #=> 8


35
36
37
# File 'lib/gadgeto/time_of_day.rb', line 35

def hour
  @hour
end

#minuteObject (readonly)

Returns the hour component.

t = TimeOfDay.new("08:03") #=> 08:00
t.minute                   #=> 3


43
44
45
# File 'lib/gadgeto/time_of_day.rb', line 43

def minute
  @minute
end

Class Method Details

.valid?(time_of_day) ⇒ Boolean

Returns true if time_of_day represents time of day.

TimeOfDay.valid?("08:00") #=> true

Returns:

  • (Boolean)


130
131
132
133
134
135
136
137
# File 'lib/gadgeto/time_of_day.rb', line 130

def self.valid?(time_of_day)
  begin
    val = TimeOfDay.new(time_of_day)
    true
  rescue InvalidTimeOfDayFormat, TimeOfDayOutOfRange
    false
  end
end

Instance Method Details

#<(other) ⇒ Object

Returns true if self precedes other.

TimeOfDay.new("08:00") < TimeOfDay.new("09:00:) #=> true


82
83
84
85
86
87
# File 'lib/gadgeto/time_of_day.rb', line 82

def <(other)
  return true if self.hour < other.hour
  return false if self.hour > other.hour

  self.minute < other.minute
end

#==(other) ⇒ Object

Returns true if self is equal to other.

TimeOfDay.new("08:00") == TimeOfDay.new("08:00") #=> true


102
103
104
# File 'lib/gadgeto/time_of_day.rb', line 102

def ==(other)
  self.hour == other.hour && self.minute == other.minute
end

#>(other) ⇒ Object

Returns true if self follows other.

TimeOfDay.new("09:00") < TimeOfDay.new("08:00:) #=> true


92
93
94
95
96
97
# File 'lib/gadgeto/time_of_day.rb', line 92

def >(other)
  return true if self.hour > other.hour
  return false if self.hour < other.hour

  self.minute > other.minute
end

#add_minutes(minutes) ⇒ Object

Adds the given minutes to self.

t = TimeOfDay.new("08:00") #=> 08:00
t.add_minutes(60)          #=> 09:00

t = TimeOfDay.new("23:50") #=> 08:00
t.add_minutes(20)          #=> 00:10


54
55
56
57
58
59
60
61
62
63
# File 'lib/gadgeto/time_of_day.rb', line 54

def add_minutes(minutes)
  new_minutes = @minute + minutes
  hours_to_add = (@minute + minutes) / 60
  new_hour = @hour + hours_to_add

  @hour = new_hour % HOURS_ONE_DAY
  @minute = new_minutes % ONE_HOUR_IN_MINUTES

  self
end

#minutes_till(other, options = {}) ⇒ Object

Returns difference in minutes between self and other.

TimeOfDay.new("08:00").minutes_till(TimeOfDay.new("09:00"))                     #=> 60
TimeOfDay.new("23:00").minutes_till(TimeOfDay.new("01:00"))                     #=> 120

TimeOfDay.new("23:00").minutes_till(TimeOfDay.new("01:00"), :overflow => false) #=> raises TimeOfDayOutOfRange


112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/gadgeto/time_of_day.rb', line 112

def minutes_till(other, options = {})
  options[:overflow] = true if options[:overflow].nil?

  if options[:overflow]
    if self > other
      (self.minutes_till(TimeOfDay.new("24:00"))) + other.to_i
    else
      other.to_i - self.to_i
    end
  else
    raise TimeOfDayOutOfRange if self > other
    other.to_i - self.to_i
  end
end

#to_iObject

Returns time of day in minutes.

TimeOfDay.new("02:07") #=> 127


75
76
77
# File 'lib/gadgeto/time_of_day.rb', line 75

def to_i
  hour * 60 + minute
end

#to_sObject Also known as: inspect

Returns string representing time of day.

TimeOfDay.new("08:00").to_s #=> "08:00"


68
69
70
# File 'lib/gadgeto/time_of_day.rb', line 68

def to_s
  "#{hour.to_s.rjust(2, '0')}:#{minute.to_s.rjust(2, '0')}"
end