Class: TimeOfDay
- Inherits:
-
Object
show all
- Includes:
- Comparable
- 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.
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/time_of_day.rb', line 39
def initialize(hour, minute = 0, second = 0)
if hour == 24
unless minute == 0 && second == 0
raise "Invalid TimeOfDay. #{hour}:#{minute}:#{second} given, but highest allowed value is 24:00:00"
end
else
raise "Invalid hour: #{hour}" unless hour >= 0 && hour <= 23
end
raise "Invalid minute: #{minute}" unless minute >= 0 && minute <= 59
raise "Invalid second: #{second}" unless second >= 0 && second <= 59
@hour = hour
@minute = minute
@second = second
end
|
Instance Attribute Details
#hour ⇒ Object
8
9
10
|
# File 'lib/time_of_day.rb', line 8
def hour
@hour
end
|
#minute ⇒ Object
9
10
11
|
# File 'lib/time_of_day.rb', line 9
def minute
@minute
end
|
#second ⇒ Object
10
11
12
|
# File 'lib/time_of_day.rb', line 10
def second
@second
end
|
Class Method Details
._parse(string) ⇒ Object
25
26
27
28
29
30
|
# File 'lib/time_of_day.rb', line 25
def self._parse(string)
parts = parse_parts(string)
return unless parts
new(*parts)
end
|
.now ⇒ Object
12
13
14
|
# File 'lib/time_of_day.rb', line 12
def self.now
Time.now.time_of_day end
|
.parse(string) ⇒ Object
16
17
18
19
20
21
22
23
|
# File 'lib/time_of_day.rb', line 16
def self.parse(string)
return nil if string.blank?
tod = _parse(string)
raise ArgumentError, "Illegal time format: '#{string}'" unless tod
tod
end
|
.parse_parts(string) ⇒ Object
32
33
34
35
36
37
|
# File 'lib/time_of_day.rb', line 32
def self.parse_parts(string)
return nil if string.blank?
return unless /^(?<hours>\d{1,2}):?(?<minutes>\d{2})?(?::(?<seconds>\d{1,2}))?$/ =~ string.strip
[hours.to_i, minutes.to_i, seconds.to_i]
end
|
Instance Method Details
#+(other) ⇒ Object
85
86
87
88
89
90
91
|
# File 'lib/time_of_day.rb', line 85
def +(other)
raise "Illegal argument: #{other.inspect}" unless other.is_a? Numeric
t = Time.local(0, 1, 1, hour, minute, second) t += other
self.class.new(t.hour, t.min, t.sec)
end
|
#-(other) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/time_of_day.rb', line 93
def -(other)
case other
when TimeOfDay
t1 = Time.local(0, 1, 1, hour, minute, second) t2 = Time.local(0, 1, 1, other.hour, other.minute, other.second) (t1 - t2).seconds
when Numeric
self + -other
else
raise "Illegal argument: #{other.inspect}"
end
end
|
#<=>(other) ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/time_of_day.rb', line 106
def <=>(other)
return -1 unless other
other_tod = if other.is_a?(TimeOfDay)
other
elsif other.respond_to?(:time_of_day)
other.time_of_day
else
other
end
to_a <=> [other_tod.hour, other_tod.minute, other_tod.second]
end
|
#change ⇒ Object
77
78
79
|
# File 'lib/time_of_day.rb', line 77
def change(*)
self
end
|
#encode_with(coder) ⇒ Object
60
61
62
63
|
# File 'lib/time_of_day.rb', line 60
def encode_with(coder)
coder.tag = 'tag:yaml.org,2002:time'
coder.scalar = to_s
end
|
#eql?(other) ⇒ Boolean
Referring to the same H/M/S makes objects equal.
127
128
129
|
# File 'lib/time_of_day.rb', line 127
def eql?(other)
hash == other.hash
end
|
#hash ⇒ Object
Referring to the same H/M/S makes objects equal. and we only want one hash key.
122
123
124
|
# File 'lib/time_of_day.rb', line 122
def hash
@hour.hash ^ @minute.hash ^ @second.hash
end
|
#in_time_zone ⇒ Object
65
66
67
|
# File 'lib/time_of_day.rb', line 65
def in_time_zone(*)
self
end
|
#init_with(coder) ⇒ Object
55
56
57
58
|
# File 'lib/time_of_day.rb', line 55
def init_with(coder)
parts = self.class.parse_parts(coder.scalar)
initialize(*parts)
end
|
#inspect ⇒ Object
146
147
148
|
# File 'lib/time_of_day.rb', line 146
def inspect
"#<#{self.class} hour=#{@hour}, minute=#{@minute}, second=#{@second}>"
end
|
#on(date) ⇒ Object
73
74
75
|
# File 'lib/time_of_day.rb', line 73
def on(date)
Time.local(date.year, date.month, date.day, hour, minute, second) end
|
#strftime(format) ⇒ Object
131
132
133
|
# File 'lib/time_of_day.rb', line 131
def strftime(format)
on(Date.today).strftime(format)
end
|
#to_a ⇒ Object
150
151
152
|
# File 'lib/time_of_day.rb', line 150
def to_a
[@hour, @minute, @second]
end
|
#to_json ⇒ Object
154
155
156
|
# File 'lib/time_of_day.rb', line 154
def to_json(*)
%("#{self}")
end
|
#to_s(with_seconds = true) ⇒ Object
Also known as:
to_fs
135
136
137
138
139
140
141
142
143
|
# File 'lib/time_of_day.rb', line 135
def to_s(with_seconds = true)
if with_seconds
'%02d:%02d:%02d' % to_a
else
'%02d:%02d' % [@hour, @minute]
end
rescue
"#{@hour.inspect}:#{@minute.inspect}:#{@second.inspect}"
end
|
#to_time ⇒ Object
69
70
71
|
# File 'lib/time_of_day.rb', line 69
def to_time
on Date.today
end
|
#utc? ⇒ Boolean
158
159
160
|
# File 'lib/time_of_day.rb', line 158
def utc?
true
end
|
#year ⇒ Object
81
82
83
|
# File 'lib/time_of_day.rb', line 81
def year
2000
end
|