Re: Duration

Jack Waugh
Wed, 22 Dec 2010 06:41:25 -0000

"Just specify units and you''ll be fine."

That appeals to my notion of what is logical.  Let''s try it.

>> x = DateTime.now
=> Wed, 22 Dec 2010 01:30:36 -0500
>> y = x + 1.day
=> Thu, 23 Dec 2010 01:30:36 -0500

So far, so good.

>> (y - x) / 1.second
=> Rational(28799745541, 28800000000)

Since y is one day later than x, y - x should be a day.  Dividing that by 1
second should yield a pure number equal to the number of seconds in a day.

>> ((y - x) / 1.second).to_i
=> 0

Even if coerced to an integer, the number of seconds in a day should be much
more than zero.

I used units, but the result differs from what seems to me to make sense.
How can I safely represent a duration in the database and then use the
duration in date arithmetic?

--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org