Duration

Jack Waugh
Tue, 21 Dec 2010 17:17:00 -0000

It looks as though if you ask a DateTime to add something to itself,
it looks at the something, and if it''s an integer, it interprets it as
a duration in seconds, but if it''s a Rational, it interprets it as a
duration in days.

It''s inconsistent.  Why didn''t the designers settle on one
type for
durations and one way to interpret them?  What should one store in a
database for a duration?  What rules should one follow to avoid being
tripped up by numbers being interpreted as different measures
depending on accidental features such as the numbers''
representations?  This is very disappointing; engineering designs
should make logical sense.

>> x = DateTime.now
=> Tue, 21 Dec 2010 12:10:14 -0500
>> d1 = 1.second
=> 1 second
>> y = x + d1
=> Tue, 21 Dec 2010 12:10:15 -0500
>> y.class
=> DateTime
>> y - x
=> Rational(1, 86400)
>> d2 = y - x
=> Rational(1, 86400)
>> d1.class
=> Fixnum
>> d1
=> 1 second
>> 0 + d1
=> 1
>> d1 == d2
=> false
>> (x + d1) == (x + d2)
=> true

--
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