monger has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Fellow Monestary Inhabitants, I come, humbly beseeching the assistance of the Monks, Saints, Prelates, etc on a Date, particularly POSIX::strftime, problem I've found.

The Setup

I ahve a perl script that runds on a Win2K box via scheduler and parses some network logs. The way it knows what to do is get today's date with

my $time = POSIX::strftime "%Y%m%d",localtime;

and them the following happens to parse yesterday's files:

my $newtime = ($time -1); my $log = $newtime.".log";

As many of you already can see, things broke this morning because 20041001 -1 is 20041000 and not 20040930. So, I'm looking for a fix. I've dug a bit through CPAN and looked at Activestates's info. Many possible modules don't want to compile properly on Win32.

Another complication, I use pp to package my scripts into Win32 executables.

Thanks for any and all assistance,

monger

Monger +++++++++++++++++++++++++ Munging Perl on the side

Replies are listed 'Best First'.
Re: Date Trickiness
by VSarkiss (Monsignor) on Oct 01, 2004 at 18:11 UTC

    Here's another way:

    my $time = POSIX::strftime "%Y%m%d",localtime; sleep 86400; # wait until tomorrow :-) my $log = $time . ".log";
    I can't take credit for this, it comes from dominus's wonderful Infrequently asked questions list.

    Update
    Well, at least I thought it was. I can't seem to find it on there anymore....

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Date Trickiness
by blokhead (Monsignor) on Oct 01, 2004 at 18:13 UTC
    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

      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
Re: Date Trickiness
by bmann (Priest) on Oct 01, 2004 at 18:06 UTC
    Use localtime to get the date 24 hours ago

    my $newtime= POSIX::strftime "%Y%m%d", localtime(time - 60 * 60 * 24);

      Note that this can go wrong at DST changes, when a day is shorter or longer than 24 hours. If you want it to work on those days to, either use gmtime instead of localtime, run the script during daytime (or at least, not between 23:00 and 1:00), or use a module (such as DateTime).
      Thanks bmann. That did the trick. monger
      Monger +++++++++++++++++++++++++ Munging Perl on the side
Re: Date Trickiness
by Zed_Lopez (Chaplain) on Oct 01, 2004 at 18:04 UTC
Re: Date Trickiness
by johnnywang (Priest) on Oct 01, 2004 at 19:39 UTC
    I always use Date::Manip, it actually understands words like "today" and "yesterday":
    use Date::Manip; my $today = UnixDate( ParseDate("today"), "%Y%m%d"); my $yesterday = UnixDate( ParseDate("yesterday"), "%Y%m%d");