Module: Doing::ChronifyNumeric

Included in:
Numeric
Defined in:
lib/doing/chronify/numeric.rb

Overview

Number helpers

Instance Method Summary collapse

Instance Method Details

#format_time(human: false) ⇒ Object

Format human readable time from seconds

Parameters:

  • human (Boolean) (defaults to: false)

    if True, don't convert hours into days



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/doing/chronify/numeric.rb', line 14

def format_time(human: false)
  return [0, 0, 0] if nil?

  seconds = dup.to_i
  minutes = (seconds / 60).to_i
  hours = (minutes / 60).to_i
  if human
    minutes = (minutes % 60).to_i
    [0, hours, minutes]
  else
    days = (hours / 24).to_i
    hours = (hours % 24).to_i
    minutes = (minutes % 60).to_i
    [days, hours, minutes]
  end
end

#time_string(format: :dhm) ⇒ Object

Format seconds as natural language time string

Parameters:

  • format (Symbol) (defaults to: :dhm)

    The format to output (:dhm, :hm, :m, :clock, :natural)



37
38
39
# File 'lib/doing/chronify/numeric.rb', line 37

def time_string(format: :dhm)
  format_time(human: true).time_string(format: format)
end