Class: Data::LocalTime
- Inherits:
-
Object
- Object
- Data::LocalTime
- Includes:
- Comparable
- Defined in:
- lib/barometer/data/local_time.rb
Overview
A simple Time class
A time class that represents the local time … it has no concept of time zone or date
Direct Known Subclasses
Instance Attribute Summary collapse
-
#hour ⇒ Object
Returns the value of attribute hour.
-
#min ⇒ Object
Returns the value of attribute min.
-
#sec ⇒ Object
Returns the value of attribute sec.
Class Method Summary collapse
Instance Method Summary collapse
- #+(seconds) ⇒ Object
- #-(seconds) ⇒ Object
- #<=>(other) ⇒ Object
- #diff(other) ⇒ Object
-
#initialize(h = 0, m = 0, s = 0) ⇒ LocalTime
constructor
A new instance of LocalTime.
- #nil? ⇒ Boolean
- #parse(string) ⇒ Object
- #to_s(seconds = false) ⇒ Object
-
#to_t ⇒ Object
convert to a Time class.
- #total_seconds ⇒ Object
Constructor Details
#initialize(h = 0, m = 0, s = 0) ⇒ LocalTime
Returns a new instance of LocalTime.
13 14 15 16 17 18 |
# File 'lib/barometer/data/local_time.rb', line 13 def initialize(h=0,m=0,s=0) self.hour = h self.min = m self.sec = s self end |
Instance Attribute Details
#hour ⇒ Object
Returns the value of attribute hour.
11 12 13 |
# File 'lib/barometer/data/local_time.rb', line 11 def hour @hour end |
#min ⇒ Object
Returns the value of attribute min.
11 12 13 |
# File 'lib/barometer/data/local_time.rb', line 11 def min @min end |
#sec ⇒ Object
Returns the value of attribute sec.
11 12 13 |
# File 'lib/barometer/data/local_time.rb', line 11 def sec @sec end |
Class Method Details
.parse(string) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/barometer/data/local_time.rb', line 68 def self.parse(string) return string if string.is_a?(Data::LocalTime) local = Data::LocalTime.new local.parse(string) local end |
Instance Method Details
#+(seconds) ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/barometer/data/local_time.rb', line 96 def +(seconds) local_time = Data::LocalTime.new if seconds.is_a?(Fixnum) || seconds.is_a?(Float) local_time.sec = self.total_seconds + seconds.to_i elsif seconds.is_a?(Data::LocalTime) this_total = self.total_seconds + seconds.total_seconds local_time.sec = this_total end local_time end |
#-(seconds) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/barometer/data/local_time.rb', line 107 def -(seconds) local_time = Data::LocalTime.new if seconds.is_a?(Fixnum) || seconds.is_a?(Float) local_time.sec = self.total_seconds - seconds.to_i elsif seconds.is_a?(Data::LocalTime) #self.sec += seconds.total_seconds this_total = self.total_seconds - seconds.total_seconds local_time.sec = this_total end local_time end |
#<=>(other) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/barometer/data/local_time.rb', line 86 def <=>(other) if other.is_a?(String) || other.is_a?(Time) || other.is_a?(DateTime) the_other = Data::LocalTime.parse(other) else the_other = other end raise ArgumentError unless the_other.is_a?(Data::LocalTime) total_seconds <=> the_other.total_seconds end |
#diff(other) ⇒ Object
119 120 121 122 123 |
# File 'lib/barometer/data/local_time.rb', line 119 def diff(other) the_other = Data::LocalTime.parse(other) raise ArgumentError unless the_other.is_a?(Data::LocalTime) (self.total_seconds - the_other.total_seconds).to_i.abs end |
#nil? ⇒ Boolean
131 |
# File 'lib/barometer/data/local_time.rb', line 131 def nil?; @hour == 0 && @min == 0 && @sec == 0; end |
#parse(string) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/barometer/data/local_time.rb', line 54 def parse(string) if string.is_a?(Time) || string.is_a?(DateTime) @hour = string.hour @min = string.min @sec = string.sec elsif string.is_a?(String) time = Time.parse(string) @hour = time.hour @min = time.min @sec = time.sec end self end |
#to_s(seconds = false) ⇒ Object
125 126 127 128 129 |
# File 'lib/barometer/data/local_time.rb', line 125 def to_s(seconds=false) time = self.to_t format = (seconds ? "%I:%M:%S %p" : "%I:%M %p") time.strftime(format).downcase end |
#to_t ⇒ Object
convert to a Time class
77 78 79 80 |
# File 'lib/barometer/data/local_time.rb', line 77 def to_t date = Date.today Time.local(date.year,date.month,date.day,@hour,@min,@sec) end |
#total_seconds ⇒ Object
82 83 84 |
# File 'lib/barometer/data/local_time.rb', line 82 def total_seconds (@hour * 60 * 60) + (@min * 60) + @sec end |