Cody Fendant has asked for the wisdom of the Perl Monks concerning the following question:
Oh the shame: my date objects weren't actually differing by more than one day because I'm an idiot and assumed that my inputs were what I thought they were! Thanks commenters for forcing me to go back and look at exactly what was in the objects. I leave this here as a memorial, or it can be reaped.
This is a bug in an application I'm working on.
The code creates two DateTime objects and subtracts one from the other to get a DateTime::Duration object.
However the DateTime::Duration object can't return a difference in number of days, which is what I need.
This is actually expected behaviour:
The last example demonstrates that there will not be any conversion between units which don't have a fixed conversion rate.my $dur = DateTime::Duration->new( years => 1, months => 15 ); $dur->in_units('years'); # 2 $dur->in_units('months'); # 27 $dur->in_units( 'years', 'months' ); # (2, 3) $dur->in_units( 'weeks', 'days' ); # (0, 0) !
I guess my question is, how do I get the difference in days between two DateTime objects?
Updated: The current code just does
Where both DateTime objects are specific fixed dates, likemy $diff = $dt1 - $dt2;
I can understand that if I create a duration object with years => 1, months => 15 that's not something where we can be sure of the number of days, but that's not what's happening in my code.
|
|---|