I think the key to Perrin's issue is documented on the DateTime
page, even though these functions were not referenced in the two posts:

+ years, months, weeks, days, hours, minutes, seconds, nanoseconds 

These methods return numbers indicating how many of the given unit the
object represents, after having done a conversion to any larger units.
For example, days are first converted to weeks, and then the remainder
is returned. These numbers are always positive.

For example:

my $d1 = DateTime->today;
my $d2 = $d1->clone->subtract(years => 1);

my $dur = $d1->delta_days($d2);
print $dur->weeks, " weeks, ", $dur->days, " days\n";

52 weeks, 2 days

Or is this a bug in the delta_days return and weeks conversion shouldn't
be happening?
        Now, I'd never used durations like this before, so this was a
good learning experience for me and perhaps this next question is my
inexperience showing, but the semantics seem contradictive.  If I were
just discovering DT I think I would expect a function named delta_days
to simply return the delta in days.  That is what the function name
seems to imply...not a duration object whose member functions have to be
used to get the days and then you need to understand the underlying math
to make sure you're getting the result you desire.
        To try to resolve this, I read up on durations a bit more.  I
think there may be a couple of documentation errors that makes this
confusing.  On the DateTime::Duration page
(http://search.cpan.org/~drolsky/DateTime-0.4501/lib/DateTime/Duration.p
m) it states:

+ delta_months, delta_days, delta_minutes, delta_seconds,
delta_nanoseconds

These methods provide the information DateTime.pm needs for doing date
math. The numbers returned may be positive or negative.

Yet on the referring page
(http://search.cpan.org/~drolsky/DateTime-0.4501/lib/DateTime.pm) it
stresses that negative returns are not provided by delta_days:

+ $dt->delta_md( $datetime ) 
+ $dt->delta_days( $datetime ) 

Each of these methods returns a new DateTime::Duration object
representing some portion of the difference between two datetimes. The
delta_md() method returns a duration which contains only the month and
day portions of the duration is represented. The delta_days() method
returns a duration which contains only days.

The delta_md and delta_days methods truncate the duration so that any
fractional portion of a day is ignored. Both of these methods operate on
the date portion of a datetime only, and so effectively ignore the time
zone.

Unlike the subtraction methods, these methods always return a positive
(or zero) duration.


        So does delta_days return a negative...not according to my
testing.  I think the duration page needs updating to indicate this.  I
also think that the reference in the days function to a duration first
being converted to weeks before returning the # of days needs to be
added to the delta_days documentation.  Or again, is this a bug in the
delta_days return and weeks conversion shouldn't be happening?

Bobby


> -----Original Message-----
> From: Alex Teslik [mailto:a...@acatysmoof.com]
> Sent: Monday, December 15, 2008 6:29 PM
> To: Perrin Harkins; datetime@perl.org
> Subject: Re: difference in absolute days
> 
> On Mon, 15 Dec 2008 20:04:26 -0500, Perrin Harkins wrote
> 
> > my $d1 = DateTime->today;
> > my $d2 = $d1->clone->subtract(years => 1);
> >
> > my $dur = $d1->delta_days($d2);
> > print $dur->in_units('days') . "\n";
> >
> 
> corrected above. I don't know why the days method didn't work.
> 
> HTH,
> Alex

Reply via email to