Dave Rolsky wrote:
> Currently, the default when subtracting datetimes is to break down the
> duration into multiple parts, months, days, minutes, seconds, and
> nanoseconds.

> From the months piece we can derive years, and from the days piece we
can
> derive weeks.

Except, as Flavio points out, for non-gregorian calendars.

> There's also the subtract_datetime_absolute method, which just returns
> seconds and nanoseconds.  I see the primary purpose of this method as
> returning an object which can be used to repeatedly add or subtract a
very
> specific absolute length duration.

Yup, good idea

> But some people have indicated that they'd like something a little
more
> flexible.  Eugene van der Pijll suggested something like this:
> 
>  my $dur = $dt1->difference( datetime => $dt2,
>                              units    => [ 'months', 'days' ] );
>
> This would return a duration which only included month and day values,
> without minutes, seconds, or nanoseconds.

$dt1 = 2003-05-15 19:23:33;
$dt2 = 2003-04-01 00:00:00;
my $dur = $dt1->difference( datetime => $dt2,
                            units    => [ 'months', 'days' ] );

So do we now have:
1 month, 15 days
or:
1 month, 15 days, 19 hours, 23 minutes, 33 seconds
or:
1 month, 15 days, 68400 seconds


> Do we really need such a flexible API?  For example, will anyone ever
want
> to do this:
> 
>  my $dur = $dt1->difference( datetime => $dt2,
>                              units    => [ 'months', 'nanoseconds' ]
);
> 
> My guess is that the answer here is no.

I'm with you .. doubt it!

> So perhaps rather than providing the above API, we should instead
offer
> something like this (taking a page from Date::Calc):

> It seems to me that these 3 cover all the important possibilities, and
> they have a nice simple API.

Looks good to me ... however for _standard_ subtraction I'd like it
stored the way I think about it:

2004-04-11 - 2003-04-20 = 1 year, -9 days.

I know this breaks some of the internal logic so far as 'if one element
is negative, it's a negative duration', but I still think of the
difference in terms of the individual units.



Also, I said this a while back so skip it if you're bored:

I'd like a 'normalise' (*normalize = \&normalise) function so that:

(1 year, -9 days)->normalise(2004-04-11) = (1 year, 11 months, 22 days)

and:
(45 days)->normalise(2004-04-11) = (1 month, 15 days);
(45 days)->normalise(2004-05-11) = (1 month, 14 days);

(45 days)->normalise(2003-02-01) = (1 month, 17 days);
(45 days)->normalise(2004-02-01) = (1 month, 16 days);

In English rather than by example:
Normalise:
Redefines a duration such that the largest possible units are used based
on a given DateTime. The DateTime is used as the starting point for the
conversion. A positive duration will be converted into the time between
the DateTime and DateTime+Duration. A negative duration will be
converted into the time between DateTime and DateTime-Duration such that
it returns a negative Duration.

The DateTime would not affect the result for such normalisations as 65
minutes. There is always 60 minutes in an hour and 65 minutes is always
1 hour, 5 minutes. However the number of days in a month changes and
converting days into months requires a base DateTime.

Maybe, though, normalise fits better into something like
DateTime::Duration::Format?


Cheers!
Rick


Reply via email to