Module: RSpecTracer::TimeFormatter

Defined in:
lib/rspec_tracer/time_formatter.rb

Constant Summary collapse

DEFAULT_PRECISION =
2
SECONDS_PRECISION =
5
UNITS =
{
  second: 60,
  minute: 60,
  hour: 24,
  day: Float::INFINITY
}.freeze

Class Method Summary collapse

Class Method Details

.format_time(seconds) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rspec_tracer/time_formatter.rb', line 17

def format_time(seconds)
  return pluralize(format_duration(seconds), 'second') if seconds < 60

  formatted_duration = UNITS.each_pair.with_object([]) do |(unit, count), duration|
    next unless seconds.positive?

    seconds, remainder = seconds.divmod(count)

    next if remainder.zero?

    duration << pluralize(format_duration(remainder), unit)
  end

  formatted_duration.reverse.join(' ')
end