dpmott has asked for the wisdom of the Perl Monks concerning the following question:

Given this script:
#!perl -w use strict; use Date::Calc qw/Localtime Delta_DHMS Delta_YMDHMS/; my $now = time(); my $total = 63072000; # approx 2 years doit( $now, $now + $total ); doit( $now - $total, $now ); sub doit { my ( $start, $end ) = @_; my @ts1 = (Localtime( $start ))[0..5]; my @ts2 = (Localtime( $end ))[0..5]; my ($years, $months, $days, $hours, $mins, $secs) = Delta_YMDHMS(@ts1, @ts2); print "$years years, $months months, $days days, $hours hours, $min +s minutes\n"; }
I get the following output:
2 years, 0 months, 0 days, 0 hours, 0 minutes
2 years, 0 months, -1 days, 0 hours, 0 minutes

I really expected those two lines to be the same.

If I use the Delta_DHMS function, and remove all references to $years and $months, then the output is the same.

The documentation for Delta_YMDHMS promises that roll-over from the time difference is correctly applied to the delta-day, but I wonder if it's actually working.

Could someone please tell me what I'm doing wrong?

Thanks,
-Dave

Replies are listed 'Best First'.
Re: Problem with Date::Calc::Delta_YMDHMS?
by dpmott (Scribe) on Mar 14, 2004 at 03:01 UTC
    After reading the POD for Date::Calc::Object, I came across this tidbit:

    If "accurate mode" is switched off (when the corresponding flag is set to false), delta vectors are calculated with year and month differences.

    E.g., the difference between "1999,12,6" and "2000,6,24" is "+0 +0 +201" (plus 201 days) in accurate mode and "+1 -6 +18" (plus one year, minus 6 months, plus 18 days) when accurate mode is switched off.

    (The delta vector is calculated by simply taking the difference in years, the difference in months and the difference in days.)

    Because years and months have varying lengths in terms of days, the latter is less accurate than the former because it depends on the context of the two dates of which it represents the difference. Added to a different date, the latter delta vector may yield a different offset in terms of days.



    Argh, but that's frustrating... sorry for posting the original question in the first place. I guess I'll stick with Delta_DHMS().

    -Dave