On Fri, 14 May 2004, Danny Rathjens wrote:

> That seems a bit overcomplicated just to figure out the number of days
> between two dates, though.
>
> Is there anything wrong with using this?:
>  [EMAIL PROTECTED]:~/tst% perl -MDateTime -wle'$past = 
> DateTime->now->subtract(months=>2); print 
> DateTime->now->delta_days($past)->delta_days'
> 61

That's fine too.

> I did manage to get some inconsistencies by making the duration bigger, :)
> perl -MDateTime::Format::Duration -MDateTime -wle'$past = 
> DateTime->now->subtract(years=>7,months=>2,days=>17); $now = DateTime->now; $dur = 
> $now - $past; my $dfd = DateTime::Format::Duration->new(pattern=>"%j",base => 
> $now);print $dfd->format_duration($dur);'
> 2632
>  [EMAIL PROTECTED]:~/tst% perl -MData::Dumper -MDateTime -wle'$past = 
> DateTime->now->subtract(years=>7,months=>2,days=>17); print 
> DateTime->now->delta_days($past)->delta_days'
> 2633
> # this one uses $past instead of $now as 'base'
>  [EMAIL PROTECTED]:~/tst% perl -MDateTime::Format::Duration -MDateTime -wle'$past = 
> DateTime->now->subtract(years=>7,months=>2,days=>17); $now = DateTime->now; $dur = 
> $now - $past; my $dfd = DateTime::Format::Duration->new(pattern=>"%j",base => 
> $past);print $dfd->format_duration($dur);'
> 2629

This probably has to do with the fact that datetime math is a multi-step
operation, and it's not reversible.

When you add/subtract a duration to a datetime, it has to add each unit
separately.

First it does days, then months (which includes years), then seconds and
nanoseconds, seconds, and then minutes.

This is basically arbitrary, but it is documented.

If you don't like the order, you can always break out the steps:

 DateTime->now->subtract( years => 7 )->subtract( months => 2 )->subtract( days => 17 
);

This gets you a different answer than doing it all together.

DT::F::Duration does some very different things.


-dave

/*=======================
House Absolute Consulting
www.houseabsolute.com
=======================*/

Reply via email to