Class: Labtime
Constant Summary collapse
- DEFAULT_TIME_ZONE =
ActiveSupport::TimeZone.new("Eastern Time (US & Canada)")
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.
-
#time_zone ⇒ Object
Returns the value of attribute time_zone.
-
#year ⇒ Object
Returns the value of attribute year.
Class Method Summary collapse
- .from_decimal(decimal_labtime, year, time_zone = ActiveSupport::TimeZone.new("Eastern Time (US & Canada)")) ⇒ Object
- .from_s(str, time_params = {}, time_zone = DEFAULT_TIME_ZONE) ⇒ Object
- .from_seconds(sec_time, year, time_zone = DEFAULT_TIME_ZONE) ⇒ Object
- .parse(realtime) ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #add_seconds(sec) ⇒ Object
-
#initialize(year, hour, min, sec, time_zone = nil) ⇒ Labtime
constructor
A new instance of Labtime.
- #time_in_seconds ⇒ Object
- #to_decimal ⇒ Object
- #to_s ⇒ Object
- #to_short_s ⇒ Object
- #to_time ⇒ Object
Constructor Details
#initialize(year, hour, min, sec, time_zone = nil) ⇒ Labtime
Returns a new instance of Labtime.
72 73 74 75 76 77 78 |
# File 'lib/labtime.rb', line 72 def initialize(year, hour, min, sec, time_zone = nil) @year = year.to_i @hour = hour.to_i @min = min.to_i @sec = sec.to_i @time_zone = time_zone || DEFAULT_TIME_ZONE end |
Instance Attribute Details
#hour ⇒ Object
Returns the value of attribute hour.
10 11 12 |
# File 'lib/labtime.rb', line 10 def hour @hour end |
#min ⇒ Object
Returns the value of attribute min.
10 11 12 |
# File 'lib/labtime.rb', line 10 def min @min end |
#sec ⇒ Object
Returns the value of attribute sec.
10 11 12 |
# File 'lib/labtime.rb', line 10 def sec @sec end |
#time_zone ⇒ Object
Returns the value of attribute time_zone.
10 11 12 |
# File 'lib/labtime.rb', line 10 def time_zone @time_zone end |
#year ⇒ Object
Returns the value of attribute year.
10 11 12 |
# File 'lib/labtime.rb', line 10 def year @year end |
Class Method Details
.from_decimal(decimal_labtime, year, time_zone = ActiveSupport::TimeZone.new("Eastern Time (US & Canada)")) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/labtime.rb', line 38 def self.from_decimal(decimal_labtime, year, time_zone = ActiveSupport::TimeZone.new("Eastern Time (US & Canada)")) raise ArguementError, "No year supplied!" if year.blank? hour = decimal_labtime.to_i remainder = decimal_labtime - hour.to_f min_labtime = 60.0 * remainder min = min_labtime.to_i remainder = min_labtime - min.to_f sec = (60 * remainder).round.to_i self.new(year, hour, min, sec, time_zone) end |
.from_s(str, time_params = {}, time_zone = DEFAULT_TIME_ZONE) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/labtime.rb', line 61 def self.from_s(str, time_params = {}, time_zone = DEFAULT_TIME_ZONE) time_captures = /(\d+)\:(\d{1,2})(\:(\d{1,2}))?(\s(\d\d\d\d))?\z/.match(str).captures time_params[:hour] ||= time_captures[0] time_params[:min] ||= time_captures[1] time_params[:sec] ||= time_captures[3] time_params[:year] ||= time_captures[5] self.new(time_params[:year], time_params[:hour], time_params[:min], time_params[:sec], time_zone) end |
.from_seconds(sec_time, year, time_zone = DEFAULT_TIME_ZONE) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/labtime.rb', line 51 def self.from_seconds(sec_time, year, time_zone = DEFAULT_TIME_ZONE) hour = (sec_time / 3600.0).to_i sec_time = sec_time - (hour * 3600) min = (sec_time / 60.0).to_i sec_time = sec_time - (min * 60) sec = sec_time self.new(year, hour, min, sec, time_zone) end |
.parse(realtime) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/labtime.rb', line 13 def self.parse(realtime) # Return nil if nil parameter return nil if realtime.nil? # Make sure datetime is an ActiveSupport:TimeWithZone object raise ArgumentError, "realtime class #{realtime.class} is not ActiveSupport::TimeWithZone" unless realtime.is_a?(ActiveSupport::TimeWithZone) # year is easy year = realtime.year # Reference fo labtime is start of year Time.zone = realtime.time_zone reference_time = Time.zone.local(year) # find difference between reference and second_difference = realtime.to_i - reference_time.to_i # convert second difference to labtime hour = second_difference / 3600 min = (second_difference - (hour * 3600)) / 60 sec = (second_difference - (hour * 3600) - (min * 60)) self.new(year, hour, min, sec, realtime.time_zone) end |
Instance Method Details
#<=>(other) ⇒ Object
85 86 87 |
# File 'lib/labtime.rb', line 85 def <=>(other) to_time <=> other.to_time end |
#add_seconds(sec) ⇒ Object
101 102 103 |
# File 'lib/labtime.rb', line 101 def add_seconds(sec) self.class.from_seconds(self.time_in_seconds + sec, self.year, self.time_zone) end |
#time_in_seconds ⇒ Object
97 98 99 |
# File 'lib/labtime.rb', line 97 def time_in_seconds hour * 3600 + min * 60 + sec end |
#to_decimal ⇒ Object
105 106 107 |
# File 'lib/labtime.rb', line 105 def to_decimal hour.to_f + (min.to_f/60.0) + (sec.to_f/3600.0) end |
#to_s ⇒ Object
89 90 91 |
# File 'lib/labtime.rb', line 89 def to_s "#{year} #{hour}:#{min}:#{sec} #{time_zone.to_s}" end |
#to_short_s ⇒ Object
93 94 95 |
# File 'lib/labtime.rb', line 93 def to_short_s "#{hour}:#{min}:#{sec}" end |
#to_time ⇒ Object
80 81 82 83 |
# File 'lib/labtime.rb', line 80 def to_time reference_time = time_zone.local(year) reference_time + time_in_seconds end |