[QandAEditor's note: the answer that had used ONE_MONTH has been fixed. Thanks to swngnmonk for pointing this out]

A note on Time::Piece - While I love it, and I use it heavily, the ONE_MONTH constant is fundamentally broken, because there's no such thing as a constant value for a month.

For example:

#!/usr/bin/perl use Time::Piece; use Time::Seconds; my $now = localtime; print sprintf("Now = %s\n", $now->cdate); for (1..12) { $now += ONE_MONTH; print sprintf("Now + %02i months = %s\n", $_, $now->cdate); }

Outputs something like this:

bash-2.05b$ ./broken.pl Now = Mon May 10 15:56:13 2004 Now + 01 months = Thu Jun 10 02:25:17 2004 Now + 02 months = Sat Jul 10 12:54:21 2004 Now + 03 months = Mon Aug 9 23:23:25 2004 Now + 04 months = Thu Sep 9 09:52:29 2004 Now + 05 months = Sat Oct 9 20:21:33 2004 Now + 06 months = Tue Nov 9 05:50:37 2004 Now + 07 months = Thu Dec 9 16:19:41 2004 Now + 08 months = Sun Jan 9 02:48:45 2005 Now + 09 months = Tue Feb 8 13:17:49 2005 Now + 10 months = Thu Mar 10 23:46:53 2005 Now + 11 months = Sun Apr 10 11:15:57 2005 Now + 12 months = Tue May 10 21:45:01 2005

Originally posted as a Categorized Answer.


In reply to Re: How do I find the difference in days between two dates, in a cool perl way? by swngnmonk
in thread How do I find the difference in days between two dates, in a cool perl way? by frankus

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.