adds methods to Numeric and Time classes to support time units and
time
difference units.
EXAMPLE
harp:~ > cat a.rb
require "timeunits"
require "yaml"
now = Time::now
a = now
y "a" => a
b = now + 2.hours + 2.minutes
y "b" => b
d = b - a
%w( seconds minutes hours days ).each do |unit|
y "d.#{ unit }" => d.send(unit)
end
harp:~ > ruby a.rb
a: 2006-09-05 15:33:23.697319 -06:00
b: 2006-09-05 17:35:23.697319 -06:00
d.seconds: 7320.0
d.minutes: 122.0
d.hours: 2.03333333333333
d.days: 0.0847222222222222
On Wednesday, September 06, 2006, at 6:43 AM, wrote:
http://rubyforge.org/projects/codeforpeople/
require “timeunits”
d = b - a
d.days: 0.0847222222222222
what science finds to be nonexistent, we must accept as nonexistent;
but what
science merely does not find is a completely different matter… it
is quite
clear that there are many, many mysterious things.
h.h. the 14th dalai lama
FWIW,
Rails already does this, as does Facets, the conversions gem, and the
ruby-units gem.
The API varies a little bit.
i didn’t know they handled time differences too? i’m sure rails does not
b = a + 42.seconds
=> Tue Sep 05 19:50:53 -0400 2006
<…>
=> Sat Sep 09 13:06:58 FLE Daylight Time 2006
thanks, i realize that. this little extension does both:
harp:~ > cat a.rb
require 'timeunits'
a = Time.now.utc
b = a + 42.days # like rails, facets, etc.
p a
p b
delta = b - a
p delta.days # inverse operation
harp:~ > ruby a.rb
Wed Sep 06 14:23:29 UTC 2006
Wed Oct 18 14:23:29 UTC 2006
42.0
not that computing ‘days’ on a difference is the inverse operation (/)
as
compared to when doing in a summation (’*’).