From: Pit Capitain Date: 2005-09-27T22:19:20+09:00 Subject: Re: Time interval Gavin Kistner schrieb: > On Sep 27, 2005, at 2:51 AM, Robert Klemme wrote: >> >> 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. Recently I've been representing time intervals by two numbers: months and seconds. With this representation it is possible to differentiate between "1 year" and "365 days": 1 year = 12 months (and 0 seconds) 365 days = (0 months and) 365 * 24 * 60 * 60 seconds Regards, Pit