Module: HCl::Utility

Included in:
App, DayEntry
Defined in:
lib/hcl/utility.rb

Instance Method Summary collapse

Instance Method Details

#as_hours(hours) ⇒ String

Convert from decimal to a string of the form HH:MM.

Parameters:

  • hours (#to_f)

    number of hours in decimal

Returns:

  • (String)

    of the form “HH:MM”



7
8
9
10
# File 'lib/hcl/utility.rb', line 7

def as_hours hours
  minutes = hours.to_f * 60.0
  sprintf "%d:%02d", (minutes / 60).to_i, (minutes % 60).to_i
end

#time2float(time_string) ⇒ #to_f

Convert from a time span in hour or decimal format to a float.

Parameters:

  • time_string (String)

    either “M:MM” or decimal

Returns:

  • (#to_f)

    converted to a floating-point number



16
17
18
19
20
21
22
23
# File 'lib/hcl/utility.rb', line 16

def time2float time_string
  if time_string =~ /:/
    hours, minutes = time_string.split(':')
    hours.to_f + (minutes.to_f / 60.0)
  else
    time_string.to_f
  end
end