Skip to content

Instantly share code, notes, and snippets.

@AndrewVos
Created January 30, 2011 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewVos/803044 to your computer and use it in GitHub Desktop.
Save AndrewVos/803044 to your computer and use it in GitHub Desktop.
class TimeHelper
class << self
def time_difference(time_start, time_end)
total_seconds = Integer(time_end - time_start)
seconds = (total_seconds % 60)
minutes = (total_seconds - seconds) / 60
hours = (minutes - (minutes % 60)) / 60
minutes = minutes - hours * 60
result = []
result << get_string_for_number("hour", hours) if hours > 0
result << get_string_for_number("minute", minutes) if minutes > 0
result << get_string_for_number("second", seconds) if seconds > 0
result.join(" ")
end
def get_string_for_number(name, number)
result = number.to_s + " " + name
result = result + "s" if number != 1
result
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment