Class: MidwireCommon::TimeTool

Inherits:
Object
  • Object
show all
Defined in:
lib/midwire_common/time_tool.rb

Overview

A useful mixing for Time behavior

Class Method Summary collapse

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
33
# File 'lib/midwire_common/time_tool.rb', line 21

def seconds_to_time(seconds)
  return 'unknown' if seconds.nil?
  tsec = seconds
  time = ''
  2.downto(0) do |int|
    power = to_the_power(60, int)
    tmp = tsec / power
    tsec -= tmp * power
    time += ':' unless time.empty?
    time += format('%02d', tmp)
  end
  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 time_to_seconds(time)
  return -1 if time.nil? || time.strip.empty?
  times = time.split(/:/).reverse
  seconds = 0
  (0...times.length).each_with_index do |int|
    seconds += times[int].to_i * (60**int)
  end
  seconds
end