Class: TimeOfDay

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hour, minute = 0, second = 0) ⇒ TimeOfDay

Returns a new instance of TimeOfDay.



10
11
12
13
14
15
# File 'lib/time_of_day.rb', line 10

def initialize(hour, minute = 0, second = 0)
  raise "Invalid hour: #{hour}" unless hour >= 0 && hour <= 23
  raise "Invalid minute: #{minute}" unless minute >= 0 && minute <= 59
  raise "Invalid second: #{second}" unless second >= 0 && hour <= 59
  @hour, @minute, @second = hour, minute, second
end

Instance Attribute Details

#hourObject

0 - 23



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

def hour
  @hour
end

#minuteObject

0 - 59



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

def minute
  @minute
end

#secondObject

0 - 59



8
9
10
# File 'lib/time_of_day.rb', line 8

def second
  @second
end

Class Method Details

.nowObject



22
23
24
# File 'lib/time_of_day.rb', line 22

def self.now
  Time.now.time_of_day
end

.parse(string) ⇒ Object



26
27
28
29
# File 'lib/time_of_day.rb', line 26

def self.parse(string)
  return nil if string.blank?
  self.new(*parse_parts(string))
end

.parse_parts(string) ⇒ Object



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

def self.parse_parts(string)
  string.strip!
  raise "Illegal time format: '#{string}'" unless string =~ /^(\d{1,2}):?(\d{2})?(?::(\d{1,2}))?$/
  [$1.to_i, $2.to_i, $3.to_i]
end

.yaml_new(klass, tag, val) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/time_of_day.rb', line 94

def self.yaml_new(klass, tag, val)
  if String === val
    self.parse val
  else
    raise YAML::TypeError, "Invalid Time: " + val.inspect
  end
end

Instance Method Details

#+(seconds) ⇒ Object



41
42
43
44
45
46
# File 'lib/time_of_day.rb', line 41

def +(seconds)
  raise "Illegal argument: #{seconds.inspect}" unless seconds.is_a? Numeric
  t = Time.local(0, 1, 1, hour, minute, second)
  t += seconds
  self.class.new(t.hour, t.min, t.sec)
end

#-(seconds) ⇒ Object



48
49
50
51
# File 'lib/time_of_day.rb', line 48

def -(seconds)
  raise "Illegal argument: #{seconds.inspect}" unless seconds.is_a? Numeric
  self.+(-seconds)
end

#<(other) ⇒ Object



62
63
64
65
# File 'lib/time_of_day.rb', line 62

def <(other)
  return false unless other.is_a? TimeOfDay
  (self <=> other) < 0
end

#<=(other) ⇒ Object



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

def <=(other)
  return false unless other.is_a? TimeOfDay
  (self <=> other) <= 0
end

#<=>(other) ⇒ Object



53
54
55
# File 'lib/time_of_day.rb', line 53

def <=>(other)
  [@hour, @minute, @second] <=> [other.hour, other.minute, other.second]
end

#==(other) ⇒ Object



57
58
59
60
# File 'lib/time_of_day.rb', line 57

def ==(other)
  return false unless other.is_a? TimeOfDay
  (self <=> other) == 0
end

#>(other) ⇒ Object



72
73
74
75
# File 'lib/time_of_day.rb', line 72

def >(other)
  return false unless other.is_a? TimeOfDay
  (self <=> other) > 0
end

#>=(other) ⇒ Object



77
78
79
80
# File 'lib/time_of_day.rb', line 77

def >=(other)
  return false unless other.is_a? TimeOfDay
  (self <=> other) >= 0
end

#init_with(coder) ⇒ Object



17
18
19
20
# File 'lib/time_of_day.rb', line 17

def init_with(coder)
  parts = self.class.parse_parts(coder.scalar)
  initialize(*parts)
end

#on(date) ⇒ Object



37
38
39
# File 'lib/time_of_day.rb', line 37

def on(date)
  Time.local(date.year, date.month, date.day, hour, minute, second)
end

#strftime(format) ⇒ Object



82
83
84
# File 'lib/time_of_day.rb', line 82

def strftime(format)
  on(Date.today).strftime(format)
end

#to_s(with_seconds = true) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/time_of_day.rb', line 86

def to_s(with_seconds = true)
  if with_seconds
    "%02d:%02d:%02d" % [@hour, @minute, @second]
  else
    "%02d:%02d" % [@hour, @minute]
  end
end

#to_yaml(opts = {}) ⇒ Object



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

def to_yaml(opts = {})
  YAML::quick_emit(nil, opts) do |out|
    out.scalar('tag:yaml.org,2002:time', self.to_s, :plain)
  end
end