I like using Time::Local whenever I can because it's fast and a core module. By combining localtime and timelocal_nocheck, you can do simple date manipulation:

#/usr/bin/perl -l use POSIX 'strftime'; use Time::Local 'timelocal_nocheck'; my ($d, $m, $y) = (localtime)[3,4,5]; my $yesterday = timelocal_nocheck 0, 0, 0, $d - 1, $m, $y; my $fivehundred = timelocal_nocheck 0, 0, 0, $d + 500, $m, $y; print "Today: ", strftime "%Y%m%d", localtime; print "Yesterday: ", strftime "%Y%m%d", localtime $yesterday; print "In 500 days: ", strftime "%Y%m%d", localtime $fivehundred; __END__ Today: 20041001 Yesterday: 20040930 In 500 days: 20060213
I like timelocal_nocheck for simple date addition/subtraction because it will DWYM when you ask for something like "the 0th day of October" (as we do in this example) or "the 20,000th second of January." See the POD for more info & examples.

Update: added ridiculous +500 days example

blokhead


In reply to Re: Date Trickiness by blokhead
in thread Date Trickiness by monger

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.