Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Strange result of localtime()

by nite_man (Deacon)
on Sep 30, 2004 at 11:19 UTC ( #395304=perlquestion: print w/replies, xml ) Need Help??

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

Hi wise monks!

I've found a very strange result of function localtime(). Let's see simple peace of code:

for my $val ( qw(1 5 10 24) ) { print "Add $val hour ...\n"; for my $date (qw(1096600000 1099170000) ) { print "\tBefore: $date - ".localtime($date)."\n"; print "\tAfter: ".($date+$val*3600)." - ".localtime($date+$val +*3600)."\n"; } print "\n"; }
After run it I've got those results:
Add 1 hour ... Before: 1096600000 - Fri Oct 1 06:06:40 2004 After: 1096603600 - Fri Oct 1 07:06:40 2004 Before: 1099170000 - Sun Oct 31 00:00:00 2004 After: 1099173600 - Sun Oct 31 01:00:00 2004 Add 5 hour ... Before: 1096600000 - Fri Oct 1 06:06:40 2004 After: 1096618000 - Fri Oct 1 11:06:40 2004 Before: 1099170000 - Sun Oct 31 00:00:00 2004 After: 1099188000 - Sun Oct 31 04:00:00 2004 Add 10 hour ... Before: 1096600000 - Fri Oct 1 06:06:40 2004 After: 1096636000 - Fri Oct 1 16:06:40 2004 Before: 1099170000 - Sun Oct 31 00:00:00 2004 After: 1099206000 - Sun Oct 31 09:00:00 2004 Add 24 hour ... Before: 1096600000 - Fri Oct 1 06:06:40 2004 After: 1096686400 - Sat Oct 2 06:06:40 2004 Before: 1099170000 - Sun Oct 31 00:00:00 2004 After: 1099256400 - Sun Oct 31 23:00:00 2004
It's amazing! Any ideads why result is "Sun Oct 31 23:00:00 2004"? It should be "Mon Nov 1 00:00:00 2004"!

Well, maybe there is a bug in the implementation of function localtime() but possibly it happened because today is my last work day before holiday :)

---
Michael Stepanov aka nite_man

It's only my opinion and it doesn't have pretensions of absoluteness!

Replies are listed 'Best First'.
Re: Strange result of localtime()
by muntfish (Chaplain) on Sep 30, 2004 at 11:29 UTC

    According to my calendar, Oct 31 is the end of daylight saving time, a.k.a. summer time. localtime is correcting for this extra hour.

    If you use gmtime instead, it won't do that.


    s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&

      Yes, you are right, muntfish. I've forgotten about that. Thanks for your replay.

      ---
      Michael Stepanov aka nite_man

      It's only my opinion and it doesn't have pretensions of absoluteness!

Re: Strange result of localtime()
by Corion (Pope) on Sep 30, 2004 at 11:25 UTC

    localtime respects daylight savings time, therefore any date math based on that should restricted to adding 22 hours repeatedly until the date changes and then subtracting the fractional part of the day.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://395304]
Approved by Corion
Front-paged by Fletch
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2021-06-13 16:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (57 votes). Check out past polls.

    Notices?