Module: Listlace::TimeHelpers

Extended by:
TimeHelpers
Included in:
TimeHelpers
Defined in:
lib/listlace/time_helpers.rb

Instance Method Summary collapse

Instance Method Details

#format_time(seconds, options = {}) ⇒ Object

Helper method to format a number of seconds as a string like “1:03:56”.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/listlace/time_helpers.rb', line 6

def format_time(seconds, options = {})
  raise ArgumentError, "can't format negative time" if seconds < 0

  hours = seconds / 3600
  minutes = (seconds / 60) % 60
  seconds = seconds % 60

  if hours > 0
    "%d:%02d:%02d" % [hours, minutes, seconds]
  else
    "%d:%02d" % [minutes, seconds]
  end
end

#parse_time(string) ⇒ Object

Helper method to parse a string like “1:03:56” and return the number of seconds that time length represents.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
# File 'lib/listlace/time_helpers.rb', line 22

def parse_time(string)
  parts = string.split(":", -1).map(&:to_i)

  raise ArgumentError, "too many parts" if parts.length > 3
  raise ArgumentError, "can't parse negative numbers" if parts.any? { |x| x < 0 }

  parts.unshift(0) until parts.length == 3
  hours, minutes, seconds = parts
  hours * 3600 + minutes * 60 + seconds
end