Class: MidwireCommon::TimeTool
- Inherits:
-
Object
- Object
- MidwireCommon::TimeTool
- Defined in:
- lib/midwire_common/time_tool.rb
Class Method Summary collapse
-
.seconds_to_time(seconds) ⇒ Object
converts the given seconds into a time string (HH:MM:SS).
-
.time_to_seconds(time) ⇒ Object
converts the given time (HH:MM:SS) to seconds.
Class Method Details
.seconds_to_time(seconds) ⇒ Object
converts the given seconds into a time string (HH:MM:SS)
seconds the seconds to convert
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/midwire_common/time_tool.rb', line 21 def self.seconds_to_time(seconds) return "unknown" if seconds.nil? t = seconds time = "" 2.downto(0) { |i| tmp = t / (60**i) t = t - tmp * 60**i time = time + ":" if not time.empty? time = time + ("%02d" % tmp) } return time end |
.time_to_seconds(time) ⇒ Object
converts the given time (HH:MM:SS) to seconds
time the time-string
8 9 10 11 12 13 14 15 16 |
# File 'lib/midwire_common/time_tool.rb', line 8 def self.time_to_seconds(time) return -1 if time.nil? or time.strip.empty? times = time.split(/:/).reverse seconds = 0 for i in (0...times.length) seconds += times[i].to_i * (60**i) end return seconds end |