David, et. al., This topic on the FAQ page is what you need:
http://silkiwiki.org/wiki/datetime/page/FAQ%3A_Sample_Calculations#How_can_I_calculate_the_difference_in_days_between_dates_-11 The FAQ was misformatted and hid this topic. I just edited it to remedy this. Since the use of delta_days() (twice!) is so so unintuitive I suggest another method in DateTime.pm called days_apart(). sub days_apart { my ($dt1, $dt2) = @_; return abs($dt1->delta_days($dt2)->delta_days()); } DateTime::Duration is deceptively complex. It deals with a very difficult problem so it needs to be complex. However, for most of us mere mortals who deal with normal everyday date problems (and really don't care much about leap seconds) we often want to know how many days separate two dates. These two lines result in very different DateTime::Durations: my $dur1 = $dt1 - $dt2; my $dur2 = $dt1->delta_days($dt2); (print them using DateTime::Format::Duration to see) One's first instinct is to use the first method ( overloading - ) BUT getting the number of days out of $dur1 is very very tricky. I think the perldoc for DateTime::Duration needs attention. The difference between: $dur->days(); $dur->delta_days(); $dur->in_units('days'); is far from clearly explained. Jon On Jan 26, 2011, at 7:50 AM, David Steinbrunner wrote: > On 1/26/11 10:13 AM, "David Steinbrunner" <dsteinbrun...@pobox.com> wrote: > >> The results cycle from 0 to 30 rather than simply going from 0 to 100 as >> I >> would expect. When I look back at the docs I see confirmation of my >> expectations with the following example: >> >> $dur->in_units( 'months' ); # 27 >> >> This shows that years, which is a greater increment, is not interfering >> with the results as in the case with months and days in my example. To >> confirm the example in the docs all you have to do it change the two >> occurrences of "days" in my example code to "months" and you will find >> the >> results to be 0 through 100. >> >> So are my expectations incorrect or is this a bug? If this is not a bug, >> is there a way to just get the days that I'm not seeing? > > Ok, a little more digging has been done so I'll follow up. > > So I started thinking about how just calling the ->days method returns > days limited by week. I don¹t have the in my code example but all you > have to do is add a print to $duration->days in there to see what I mean. > This got me thinking about weeks and what is done for it compared to days > and months. It turns out the results of ->in_units('weeks') and ->weeks > is exactly the same, 0 through 3. > > So when calling ->months, ->weeks or ->days you get results limited to the > next highest interval but when using in_units for the same intervals each > has a different type of result. > > Months: Not limited > Weeks: Limited but the next interval (months) > Days: Limited by two intervals (months) > > Of course, the pattern here is that each of these are limited by months > but limiting months by months gives you the "right" answer so there is no > issue. > > So that is it for now. I'm assuming this is a bug but I'd appreciate > someone confirming my assumption. > > -- > David Steinbrunner > > >