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

i'd like to make a loop that writes out the last 10 days...

i was figuring on using

$i=0 while ($i < 10) { @time_array = localtime() !!minus $i++ days!!; }
of course the bit in the !! .. !! must be the magic line of code that steals one day from the perl function "localtime()"

anyone help?

Replies are listed 'Best First'.
Re: how do i get yesterday's date using localtime()?
by btrott (Parson) on Apr 26, 2000 at 22:33 UTC
    localtime takes an argument of the form returned by the time function. e.g epoch seconds. Which is just a number that you can subtract from. So
    my $now = time; for my $i (0..9) { print "$i days ago... ", scalar localtime $now - $i * 86400, "\n"; }
    should work. 86400 is the number of seconds in a day. You could also do this using Date::Manip.

    And you could get fancier w/ the date printout than this is--it's just an example.

      For a discourse (with code) on why subtracting 86400 is the wrong way to go about this in general (for two one-hour windows each year), see On the peril of discounting intuition.

      The punchline is "daylight savings time".

      If you're working backwards from a time within normal business hours, you should be O.K.

      Using btrott's example, how could you get fancier with the date. Suppose I want to break out the string into just numbers like 052901 instead of May 29, 2001.

      qball~"I have node idea?!"
Re: how do i get yesterday's date using localtime()?
by ZZamboni (Curate) on Apr 26, 2000 at 23:05 UTC
    Using Date::Manip:
    use Date::Manip; foreach (1..10) { print UnixDate(ParseDate("$_ days ago"),'%c')."\n"; }
    The '%c' format prints the date in the same format as scalar(localtime). See the documentation for the different formats you can use with UnixDate().
Re: how do i get yesterday's date using localtime()?
by egabriel (Initiate) on Apr 26, 2000 at 23:20 UTC