Class: JustTime
Overview
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.
Constant Summary collapse
- VERSION =
"0.1.1"
Instance Attribute Summary collapse
-
#ssm ⇒ Object
readonly
Seconds Since Midnight.
Class Method Summary collapse
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #<=>(other) ⇒ Object
- #hours ⇒ Object (also: #hrs, #hour)
-
#initialize(*arg) ⇒ JustTime
constructor
of class << self.
- #inspect ⇒ Object
- #minutes ⇒ Object (also: #mins, #minute)
- #seconds ⇒ Object (also: #secs, #second)
- #to_s(mode = :hhmmss) ⇒ Object
-
#to_time(date = Date.today) ⇒ Object
If you want to turn a JustTime into a Time, you might want to supply a date.
Constructor Details
#initialize(*arg) ⇒ JustTime
of class << self
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# 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 |
Instance Attribute Details
#ssm ⇒ Object (readonly)
Seconds Since Midnight
14 15 16 |
# File 'lib/just_time.rb', line 14 def ssm @ssm end |
Class Method Details
.from_time(time) ⇒ Object
18 19 20 21 22 |
# 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) ⇒ Object
44 45 46 |
# File 'lib/just_time.rb', line 44 def midnight(time=Time.now) Time.new(time.year, time.month, time.day) end |
.now ⇒ Object
39 40 41 42 |
# File 'lib/just_time.rb', line 39 def now secs = (Time.now - midnight).to_i self.new(secs) end |
.parse(string) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# 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 |
Instance Method Details
#+(other) ⇒ Object
114 115 116 117 |
# File 'lib/just_time.rb', line 114 def +(other) o = other.kind_of?(JustTime) ? other.ssm : other JustTime.new(ssm + o) end |
#-(other) ⇒ Object
109 110 111 112 |
# File 'lib/just_time.rb', line 109 def -(other) o = other.kind_of?(JustTime) ? other.ssm : other JustTime.new(ssm - o) end |
#<=>(other) ⇒ Object
119 120 121 122 |
# File 'lib/just_time.rb', line 119 def <=>(other) return 0 unless other.kind_of? JustTime ssm <=> other.ssm end |
#hours ⇒ Object Also known as: hrs, hour
77 78 79 |
# File 'lib/just_time.rb', line 77 def hours ssm / (60 * 60) end |
#inspect ⇒ Object
105 106 107 |
# File 'lib/just_time.rb', line 105 def inspect "#<JustTime #{to_s}>" end |
#minutes ⇒ Object Also known as: mins, minute
70 71 72 |
# File 'lib/just_time.rb', line 70 def minutes ssm / 60 % 60 end |
#seconds ⇒ Object Also known as: secs, second
84 85 86 |
# File 'lib/just_time.rb', line 84 def seconds ssm % 60 end |
#to_s(mode = :hhmmss) ⇒ Object
98 99 100 101 102 103 |
# 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) ⇒ Object
If you want to turn a JustTime into a Time, you might want to supply a date.
94 95 96 |
# 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 |