class JustTime
A class to represent a time object without a date Ruby doesn't have one, and I don't want to rely on, eg, Sequel.SQLTime.
Constants
- VERSION
Attributes
ssm[R]
Public Class Methods
from_time(time)
click to toggle source
# File lib/just_time.rb, line 18 def from_time(time) return nil if time.nil? secs = ( time - midnight(time) ).to_i self.new(secs) end
midnight(time=Time.now)
click to toggle source
# File lib/just_time.rb, line 44 def midnight(time=Time.now) Time.new(time.year, time.month, time.day) end
new(*arg)
click to toggle source
# File lib/just_time.rb, line 50 def initialize(*arg) fail_bad_params("new", arg) unless arg.all?{|a| a.kind_of?(Fixnum) } fail_bad_params("new", arg) if arg.size > 1 && arg.any?{|a| a < 0} @ssm = case arg.size when 1 @ssm = arg.first when 2 fail_bad_params("new", arg) if arg.first > 24 || arg.last > 59 @ssm = arg.last * 60 + arg.first * 60 * 60 when 3 fail_bad_params("new", arg) if arg[0] > 24 || arg[1] > 59 || arg[2] > 59 @ssm = arg[2] + arg[1] * 60 + arg[0] * 60 * 60 else fail_bad_params("new", arg) end end
now()
click to toggle source
# File lib/just_time.rb, line 39 def now secs = (Time.now - midnight).to_i self.new(secs) end
parse(string)
click to toggle source
# File lib/just_time.rb, line 24 def parse(string) return nil if (string.nil? || string =~ /\A\s*\Z/) begin parts = string.split(":").map{|s| Integer(s, 10) } raise if parts.size < 2 rescue raise ArgumentError, "Bad parameters passed to JustTime.parse - '#{string}'" end self.new(*parts) end
Public Instance Methods
+(other)
click to toggle source
# File lib/just_time.rb, line 114 def +(other) o = other.kind_of?(JustTime) ? other.ssm : other JustTime.new(ssm + o) end
-(other)
click to toggle source
# File lib/just_time.rb, line 109 def -(other) o = other.kind_of?(JustTime) ? other.ssm : other JustTime.new(ssm - o) end
<=>(other)
click to toggle source
# File lib/just_time.rb, line 119 def <=>(other) return 0 unless other.kind_of? JustTime ssm <=> other.ssm end
hours()
click to toggle source
# File lib/just_time.rb, line 77 def hours ssm / (60 * 60) end
inspect()
click to toggle source
# File lib/just_time.rb, line 105 def inspect "#<JustTime #{to_s}>" end
minutes()
click to toggle source
# File lib/just_time.rb, line 70 def minutes ssm / 60 % 60 end
seconds()
click to toggle source
# File lib/just_time.rb, line 84 def seconds ssm % 60 end
to_s(mode=:hhmmss)
click to toggle source
# File lib/just_time.rb, line 98 def to_s(mode=:hhmmss) case mode when :hhmm then "%02d:%02d" % [ hours, minutes ] else "%02d:%02d:%02d" % [ hours, minutes, seconds ] end end
to_time(date=Date.today)
click to toggle source
If you want to turn a JustTime
into a Time, you might want to supply a date.
# File lib/just_time.rb, line 94 def to_time(date=Date.today) Time.new(date.year, date.month, date.day, hours, minutes, seconds) end
Private Instance Methods
fail_bad_params(method, para)
click to toggle source
# File lib/just_time.rb, line 126 def fail_bad_params(method, para) raise ArgumentError, "Bad parameters passed to JustTime.#{method} - '#{para.inspect}'", caller end