in reply to Date Trickiness

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

Replies are listed 'Best First'.
Re^2: Date Trickiness
by monger (Friar) on Oct 01, 2004 at 19:07 UTC
    Thanks for the info blockhead. That'll go in for future use for sure. Used bmann's quickie, but your's is an excellent compliment. monger
    Monger +++++++++++++++++++++++++ Munging Perl on the side