class DayTime
Constants
- HMS_REGEXP
- HM_REGEXP
- SECONDS_PER_DAY
Attributes
seconds_since_midnight[R]
Public Class Methods
new(arg)
click to toggle source
# File lib/day_time.rb, line 14 def initialize(arg) @seconds_since_midnight = case arg when self.class arg.seconds_since_midnight when Time seconds_from_time(arg) when DateTime seconds_from_time(arg.to_time) when Integer seconds_from_integer(arg) when String seconds_from_time_string(arg) when Hash seconds_from_hash(arg) else raise ArgumentError end end
Public Instance Methods
+(obj)
click to toggle source
# File lib/day_time.rb, line 57 def +(obj) other = DayTime.new(obj) DayTime.new(@seconds_since_midnight + other.seconds_since_midnight) end
-(obj)
click to toggle source
# File lib/day_time.rb, line 62 def -(obj) other = DayTime.new(obj) DayTime.new((@seconds_since_midnight - other.seconds_since_midnight).abs) end
<=>(other)
click to toggle source
# File lib/day_time.rb, line 45 def <=>(other) seconds_since_midnight <=> other.seconds_since_midnight end
each() { |self| ... }
click to toggle source
# File lib/day_time.rb, line 53 def each 1.upto(Float::INFINITY) { |i| yield self + i } end
hours()
click to toggle source
# File lib/day_time.rb, line 33 def hours @seconds_since_midnight / (60 ** 2) end
minutes()
click to toggle source
# File lib/day_time.rb, line 37 def minutes (@seconds_since_midnight % 60 ** 2) / 60 end
seconds()
click to toggle source
# File lib/day_time.rb, line 41 def seconds @seconds_since_midnight % 60 end
succ()
click to toggle source
# File lib/day_time.rb, line 49 def succ self + 1 end
to_i()
click to toggle source
# File lib/day_time.rb, line 71 def to_i @seconds_since_midnight end
to_s()
click to toggle source
# File lib/day_time.rb, line 67 def to_s [hours, minutes, seconds].map { |int| int.to_s.rjust(2, "0") }.join(":") end
Private Instance Methods
seconds_from_hash(hash)
click to toggle source
# File lib/day_time.rb, line 97 def seconds_from_hash(hash) [[:hours, :h], [:minutes, :m]].each do |keys| unless keys.any? { |key| hash.key?(key) } raise ArgumentError( "Either a `#{keys[0]}` or `#{keys[2]} key is required" ) end end h = hash[:hours] || hash[:h] m = hash[:minutes] || hash[:m] s = hash[:seconds] || hash[:s] || 0 seconds_from_integer(h * 60 ** 2 + m * 60 + s) end
seconds_from_integer(seconds_since_midnight)
click to toggle source
# File lib/day_time.rb, line 77 def seconds_from_integer(seconds_since_midnight) if seconds_since_midnight > SECONDS_PER_DAY (seconds_since_midnight % SECONDS_PER_DAY) - 1 else seconds_since_midnight end end
seconds_from_time(time)
click to toggle source
# File lib/day_time.rb, line 113 def seconds_from_time(time) seconds_from_hash( h: time.hour, m: time.min, s: time.sec ) end
seconds_from_time_string(str)
click to toggle source
# File lib/day_time.rb, line 85 def seconds_from_time_string(str) h, m, s = if match = str.match(HMS_REGEXP) match.captures.map(&:to_i) elsif match = str.match(HM_REGEXP) match.captures.map(&:to_i) << 0 else raise ArgumentError end seconds_from_hash(h: h, m: m, s: s) end