The way we earthlings divide and subdivide our time is inescapably weird. DateTime protects you from some of that weirdness, but as I said, the weirdness is ultimately inescapable.
Run this:
use 5.010; use DateTimeX::Auto ':auto'; use DateTime::Format::Human::Duration; sub saydiff { state $fmt = DateTime::Format::Human::Duration::->new; say $fmt->format_duration_between(@_); } saydiff('2009-02-12', '2012-02-12'); saydiff('2009-02-12', '2012-08-12'); saydiff('2009-02-12', '2012-08-11'); saydiff('2009-02-11', '2012-08-11');
The first two results should be unsurprising:
3 years 3 years, 6 months
The next one is somewhat more odd:
3 years, 5 months, 3 weeks, 6 days
Huh? 3 years, 5 months takes you to the 12th of July; adding 3 weeks to that takes you to the 2nd of August; and then 6 more days is the 8th of August, which is still three days short!
But then the last result is:
3 years, 6 months
... again. And how did we arrive back at that? By shifting one day at the start of the duration. So we see that actually the third result was not wrong; that there are multiple correct answers!, The DateTime module was just selecting a slightly unintuitive one.
Let's go back to your example. "Now" is 00:32:13 on 4th of August 2012. DateTime claims: 3 years, 4 months, 3 weeks, 16 hours, 16 minutes, 13 seconds.
So let's backtrack 3 years: 00:32:13 on 4th of August 2009.
Backtrack 4 months: 00:32:13 on 4th of April 2009.
Backtrack 3 weeks: 00:32:13 on 14th of March 2009.
Backtrack 16 hours: 08:32:13 on 13th of March 2009.
Backtrack 16 minutes: 08:16:13 on 13th of March 2009.
Backtrack 13 seconds: 08:16:00 on 13th of March 2009.
... which is what you wanted!
So DateTime's result makes perfect sense if you count backwards; just not so much if you count forwards.
In reply to Re: Problem using DateTime
by tobyink
in thread Problem using DateTime
by zBernie
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |