Class: MidwireCommon::TimeTool

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

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/midwire_common/time_tool.rb', line 19

def self.seconds_to_time(seconds)
  return 'unknown' if seconds.nil?
  t = seconds
  time = ''
  2.downto(0) do |i|
    tmp = t / (60**i)
    t -= tmp * 60**i
    time += ':' unless time.empty?
    # rubocop:disable Style/FormatString
    time += ('%02d' % tmp)
    # rubocop:enable Style/FormatString
  end
  time
end

.time_to_seconds(time) ⇒ Object

converts the given time (HH:MM:SS) to seconds

time the time-string



6
7
8
9
10
11
12
13
14
# File 'lib/midwire_common/time_tool.rb', line 6

def self.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 |i|
    seconds += times[i].to_i * (60**i)
  end
  seconds
end