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

I have been using the following code in a number of my scripts to get the current date and/or time:
my $sec; my $min; my $hour; my $mday; my $mon; my $year; my $wday; my $yday; my $isdist; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdist) = localtime(tim +e); $year = $year + 1900; $mon = $mon + 1; if (length $mday == 1){ $mday = "0".$mday; } if (length $mon == 1){ $mon = "0".$mon; } if (length $hour == 1){ $hour = "0".$hour; } if (length $sec == 1){ $sec = "0".$sec; } my $now_string = $year.$mon.$mday;
What I would like to do though is get the previous day. Any help is appreciated, thanks.

Replies are listed 'Best First'.
•Re: localtime()
by merlyn (Sage) on Jul 16, 2004 at 20:09 UTC
    if (length $mday == 1){ $mday = "0".$mday; }
    Can we please put an end to this idiom now? I think I first saw it in a MSA program, and now am boggled at how far it has gotten. Definitely a bad book or website you got that from, sir. I'd distrust the rest of the stuff you got there.

    As for "yesterday", just subtract 86400 from "time", and do localtime() on that. It'll be wrong (by an hour) twice a year, but close enough for most purposes.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      I always found that expression odd. I usually use:

      $mday = sprintf "%02d", $mday;

      If the $mday variable is already a two digit value, no harm, and if not it now is one. Less code is nice.

      I personally use the strftime approach that blokhead shows, but I'm curious. Why do you dislike this idiom? Is it due to the length checking? The use of a two digit day? The fact that this can be done easily with sprintf? Or just the fact that the entire date format can be done more easily through strftime?
Re: localtime()
by blokhead (Monsignor) on Jul 16, 2004 at 20:17 UTC
    Instead of 25 lines to format the date, try 1:
    use POSIX 'strftime'; my $now_string = strftime( "%Y%m%d", localtime );
    For the previous day's, use localtime(time - 86400) as merlyn points out. If the daylight savings time thing will be a problem, have a look at Date::Calc or even Time::Local (in the core distribution).

    blokhead

      Thanks blockhead! And thanks to everyone for pointing out my use of "bad" code. I'm new to perl and am still learning. The two lines below are much better then what I was doing before, thanks again.
      use POSIX 'strftime'; my $now_string = strftime( "%Y%m%d", localtime(time - 86400));
Re: localtime()
by Mercio (Scribe) on Jul 17, 2004 at 00:29 UTC
    It's just trivial, but you can put the my infront of the localtime() line and get rid of the long gheto of declarations at the begining.

      Actually, while you're going about the art of fixing unperlish code, how about this?...

      my %timehash; @timehash{ qw/sec min hour mday mon year wday yday isdist/ } = ( localtime );

      Not sure if it's an improvement, but at least you've got just one lexical instead of a whole slew of them.

      Of course other more reasonable solutions have already been posted, but I just couldn't bear to see all those scalars being created and used. The hash keeps them all under one roof, so to speak.


      Dave