class TimeOfDay
Attributes
hour[RW]
minute[RW]
second[RW]
Public Class Methods
_parse(string)
click to toggle source
# File lib/time_of_day.rb, line 51 def self._parse(string) parts = parse_parts(string) return unless parts new(*parts) end
new(hour, minute = 0, second = 0)
click to toggle source
# File lib/time_of_day.rb, line 12 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
now()
click to toggle source
# File lib/time_of_day.rb, line 38 def self.now Time.now.time_of_day # rubocop: disable Rails/TimeZone end
parse(string)
click to toggle source
# File lib/time_of_day.rb, line 42 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)
click to toggle source
# File lib/time_of_day.rb, line 58 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
Public Instance Methods
+(other)
click to toggle source
# File lib/time_of_day.rb, line 81 def +(other) raise "Illegal argument: #{other.inspect}" unless other.is_a? Numeric t = Time.local(0, 1, 1, hour, minute, second) # rubocop: disable Rails/TimeZone t += other self.class.new(t.hour, t.min, t.sec) end
-(other)
click to toggle source
# File lib/time_of_day.rb, line 89 def -(other) case other when TimeOfDay t1 = Time.local(0, 1, 1, hour, minute, second) # rubocop: disable Rails/TimeZone t2 = Time.local(0, 1, 1, other.hour, other.minute, other.second) # rubocop: disable Rails/TimeZone (t1 - t2).seconds when Numeric self.+(-other) else raise "Illegal argument: #{other.inspect}" end end
<=>(other)
click to toggle source
# File lib/time_of_day.rb, line 102 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
acts_like_time?()
click to toggle source
# File lib/time_of_day.rb, line 65 def acts_like_time? true end
encode_with(coder)
click to toggle source
# File lib/time_of_day.rb, line 33 def encode_with(coder) coder.tag = 'tag:yaml.org,2002:time' coder.scalar = to_s end
eql?(other)
click to toggle source
Referring to the same H/M/S makes objects equal.
# File lib/time_of_day.rb, line 123 def eql?(other) hash == other.hash end
hash()
click to toggle source
Referring to the same H/M/S makes objects equal. and we only want one hash key.
# File lib/time_of_day.rb, line 118 def hash @hour.hash ^ @minute.hash ^ @second.hash end
in_time_zone(*)
click to toggle source
# File lib/time_of_day.rb, line 69 def in_time_zone(*) self end
init_with(coder)
click to toggle source
# File lib/time_of_day.rb, line 28 def init_with(coder) parts = self.class.parse_parts(coder.scalar) initialize(*parts) end
inspect()
click to toggle source
# File lib/time_of_day.rb, line 141 def inspect "#<#{self.class} hour=#{@hour}, minute=#{@minute}, second=#{@second}>" end
on(date)
click to toggle source
# File lib/time_of_day.rb, line 77 def on(date) Time.local(date.year, date.month, date.day, hour, minute, second) # rubocop: disable Rails/TimeZone end
strftime(format)
click to toggle source
# File lib/time_of_day.rb, line 127 def strftime(format) on(Date.today).strftime(format) end
to_a()
click to toggle source
# File lib/time_of_day.rb, line 145 def to_a [@hour, @minute, @second] end
to_json(*)
click to toggle source
# File lib/time_of_day.rb, line 149 def to_json(*) %("#{self}") end
to_s(with_seconds = true)
click to toggle source
# File lib/time_of_day.rb, line 131 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()
click to toggle source
# File lib/time_of_day.rb, line 73 def to_time on Date.today end