given a timestamp, I could like to have a method that returns the correct unit (secs, hours, minutes) and the correct metric for that unit.
Examples, for a timestamp:
1 sec ago - Unit: SEC, Metric: 1
20 secs ago - Unit: SECS, Metric: 20
1 hour ago - Unit: HOUR, Metric: 1
3 hour ago - Unit: HOURS, Metric: 3
25 hours ago - Unit: DAY, Metric: 1
50 hours ago - Unit: DAYS, Metric: 2
For this method I only need to go support, sec, hours and days. Not months. Ideas?
Most of the logic is already as a helper in Rails: http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words
Something like this maybe:
def words_and_units(from_time, to_time = 0)
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance_in_minutes = (((to_time – from_time).abs)/60).round
distance_in_seconds = ((to_time – from_time).abs).round
end