Creation of a timespan obect for simple formating of time differences. Forked from the very useful time_diff
gem 'timespan', :git => 'git://github.com/toxaq/timespan.git'
require 'timespan' time_span_from_dates = Time.diff(start_date_time, end_date_time) time_span_from_seconds = Time.new(duration_in_seconds)
This will return a Timespan object of time difference with properties of years, months, weeks, days, hours, minutes and seconds.
%y - year %M - month %w - week %d - day %H - hour %N - minute %S - second %h - hour (without adding 'hour' text to the hours. eg: 3 for 3 hours) %m - minute (without adding 'minute' text) %s - second (without adding 'second' text)
By default the format is
'%y, %M, %w, %d and %h:%m:%s'
and is called by
time_span.to_format
and will return
'1 year, 2 months, 3 weeks, 4 days and 12:05:52'.
You can pass a custom format like this
time_span.to_format('%h:%m:%s')
The output of the format is cummulative. If you give ‘%d %h’ as the format to the Timespan.to_format() method, then the output will be calculated only in days and hours.
Copyright © 2011 abhidsm. See LICENSE.txt for further details.