So there was some talk about this earlier and I was thinking about how
best to make this information available.

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.

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.

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.

This has some obvious uses, as does requesting only days when you'd rather
get back "45 days" than "1 month, 14 days".

So this makes sense to me.

_BUT_ ...

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.

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

 # months & days
 my $dur = $dt->delta_md($dt2);

 # days only
 my $dur = $dt->delta_days($dt2);

 # hours, minutes and seconds only
 my $dur = $dt->delta_hms($dt2);

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

Flavio, you'd mentioned something about wanting to get years back as a
unit, but that didn't make much sense to me.  Years is _always_ equal to
"months * 12", so if we have:

 my $dur = $dt->delta_md($dt2);

then "$dur->years" will give you what you want, right?


Anyway, comments on the above API are welcome.


-dave

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

Reply via email to