http://qs321.pair.com?node_id=63653


in reply to How to find yesterdays date (was: Date)

Okay, without using modules:
my ($day, $month, $year, $filename); (undef, undef, undef, $day, $month, $year, undef) = localtime(time() - + (24*60*60)); $filename = sprintf("%02d-%02d-%04d.txt", $day, $month+1, $year+1900);
Season to taste.

And, yes, you really should learn to use modules. They're invaluable.

Replies are listed 'Best First'.
Re: Re: Date
by dws (Chancellor) on Mar 12, 2001 at 03:01 UTC
    With the tiny caveat that this'll fail during one hour a year if you're honoring daylight savings time. If you think about this a minute or two, there's an easy workaround (which I'll leave as an exercise :)

    Retracted. See below.

      Wouldn't it fail two hours each year? If it is within the first hour of the day after a 23 hour day, 24 hours ago will be two days ago. If it is within the last hour of a 25 hour day, 24 hours ago will be the same day.
        By my calculation (which may well be wrong), subtracting 24 to determine yesterday's date is wrong during one one-hour period per year, and adding 24 hours to determine tomorrow's date is wrong during a second one-hour period, each period corresponding to one of the adjustments.

        If I'm wrong, please set me straight.

        Updated:

        I wasn't 100% sure I was right, so I did a bit of research and an experiment with Time::Local. At least in most of the U.S. (there are exceptions, dammit), we do the daylight savings switch by either repeating 1-2am on the first Sunday of April, by by skipping from 1:59:59 to 3:00:00am on the last Sunday of October. In either case, it's save to subtract 24 hours from the current time for the purposes of determining the previous date.

        Nope, I was right the first time.