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.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: Problem using DateTime by tobyink
in thread Problem using DateTime by zBernie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.