Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: date

by sierrathedog04 (Hermit)
on May 28, 2001 at 17:43 UTC ( [id://83725]=note: print w/replies, xml ) Need Help??


in reply to Yesterday's or last month's date?

Normally the solution would be simply:
my $yesterday = time() - (24 * 60 * 60); my ($day, $month, $year) = localtime($yesterday)[3,4,5];
(Remember to add one to the value of $month and 1900 to the value of $year before printing them out.)

However, yesterday's date may not be twenty-four hours ago. The day on which daylight savings time (DST) ends is 25 hours long; the day on which DST begins is only 23 hours long.

Thus if you calculated the date 24 hours earlier during the last hour of the day DST ended you would get an erroneous result using the above formula, i.e., "yesterday's" and today's dates would be the same.

Similarly if you calculated the date 24 hours earlier during the first hour of the first whole day after DST began you would also get an erroneous result using the above formula: "yesterday's" date would be two days before today's date.

'perldoc perlfaq4' has a workaround by Russ Allbery that fixes the DST problem.

Update: Corrected above to state that the formula at top would provide incorrect results in the last hour of the day DST ended, not the first hour of the day after it ended.

Replies are listed 'Best First'.
Re: Re: date
by mattr (Curate) on May 28, 2001 at 18:24 UTC
    Similar problem came up recently with a bbs running on a server in the U.S. for a Japanese audience. Daylight savings time and so on were involved. As long as you stick to unix time (seconds from epoch) you can calculate things like "24 hours ago" easily. Staying in Greenwich time until the end is useful.

    But if you want to do lots of other calculations (like I did for a recent timesheet tracking program that prints a calendar on top of a form) Date::Manip can do just about anything. Careful, it and CGI are some of the biggest modules in Perldom. (My process was 9 mb).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found