From: xmlblog Date: 2005-09-28T02:21:43+09:00 Subject: Re: Time interval While not in Ruby, I recently created a DateTime / Interval library in Javascript based on the same ideas you've all been tossing around in here. Ultimately, the language has very little impact on the design questions that have been raised and there are some surprises in working with dates as well. When I wrote the Date Extras Javascript Library, I initially took the approach of setting 30 days approx. = 1 month, 365 days approx = 1 year (based on what I saw in Rails). But IMO the methods in Rails weren't designed with the goal of high fidelity to the actual time. It seems to me that they were designed to enable cool things like time_ago_in_words. In reality, chasing down accuracy in a general purpose Date library is not an easy task. Subtle design choices like what time of day is Date.parse("12/1/2005") have big implications depending on the application. In Date Extras I chose noon as the default time for some methods because it made generating a calendar simpler. Consider that doing something like this: 365.times do |i| puts Date.today.add_days(i) end will produce unintended results due to daylight savings time if Date.today returns a date with 0 hours, minutes, seconds, and milliseconds. Using a time like 12 Noon was easy because the clocks tend to change at 1 or 2 in the morning. I'll finish up by throwing an idea out there for the Interval implementation and verturing an answer to the question of why date subtraction yields a Rational. In JS, subtrating dates yields their difference in milliseconds. This is because JS measures time as a millisecond offset from 1/1/1970. To make my life easier, I created a TimeSpan class whose initializer accepts milliseconds and provides convenience properties to access that same interval in different granularities. I also chose to use the absolute value of the milliseconds in case the client code accidentally reversed the dates, plus some might think it looks more natural to write: elvis_lived = TimeSpan.new(Date.new("1/8/1935") - Date.new("8/16/1977")).days Anyway, if you're inclined to look at the library you can find it here: http://www.xml-blog.com/articles/2005/09/19/date-extras-javascript-library-part-ii Comments, suggestions, (flames?) welcome.