From: Gavin Kistner Date: 2005-09-27T21:54:37+09:00 Subject: Re: Time interval On Sep 27, 2005, at 2:51 AM, Robert Klemme wrote: > Daniel Berger wrote: >> Robert Klemme wrote: >>> Note that it's easy to output difference in seconds, minutes, hours, >>> days and weeks but then it starts to get more difficult. >> You mean with the existing DateTime class? How? I thought this was >> possible, but my Google skills appear to be failing me at the moment. >> My own experiments weren't much better. > No, I mean because of the underlying model (our calendar). Months > have > different lengths. Same for years and quarters. For example: require 'date' d1 = DateTime.parse( "2/1/2000" ) d2 = DateTime.parse( "3/2/2000" ) puts d2-d1 #=> 30 Should the human-readable difference in that time say: 1 month, 1 day 30 days 1 month ? How about: d1 = DateTime.parse( "2/1/2000" ) d2 = DateTime.parse( "2/1/2001" ) d3 = DateTime.parse( "2/1/2002" ) puts d2-d1 #=> 366 puts d3-d2 #=> 365 Should those both say "1 year", or "1 year, 1 day" for the first? If you standardize and say "All 'months' are 30 days long, all 'years' are 365 days" then you can run into situations like the above where the obvious difference to a human is different from the computed difference. This only falls down when you try to go beyond weeks; if all your time intervals are short enough that formatting them as "3 weeks, 4 days, 5 hours, 27 minutes" is acceptable enough, then you're good to go. Alternatively, you need to keep year/month/date information along with the endpoints of your interval, and do piecewise differencing.