Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Date

by redcloud (Parson)
on Mar 12, 2001 at 00:14 UTC ( [id://63653]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://63653]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-29 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found