in reply to Is there an official DST border day detection module?

sub today_is_24hours { my $now = 60*60*int( time()/60/60 ); # Start of this hour my $hour = ( localtime($now) )[2]; $now -= $hour*60*60; # Start of this day return ( localtime($now) )[8] == ( localtime($now+24*60*60) )[8]; }

(DST never changes twice in a single day.)

- tye        

  • Comment on Re: Is there an official DST border day detection module? (localtime)
  • Download Code

Replies are listed 'Best First'.
Re^2: Is there an official DST border day detection module? (localtime)
by flowdy (Scribe) on Oct 28, 2014 at 10:17 UTC
    Hi tye,

    thanks to you a lot. As I want to get amount of hours in the day, I tried to modify your suggestion to:

    sub hours_in_day { my $now = 60*60*int( shift()/60/60 ); # Start of this hour my $hour = ( localtime($now) )[2]; $now -= $hour*60*60; # Start of this day $hour = ( localtime($now+24*60*60) )[2]; $hour -= 24 if int($hour/12); return 24-$hour; }

    But it is not working, hm ...

    Thinking & probing,
    flowdy

      I think your logic is slightly amiss when you calculate the "start of this day". This variant works for me:

      #!/usr/bin/perl -Tw use strict; print "Today: " . hours_in_day (time) . "hours.\n"; print "Sun: " . hours_in_day (time-170000) . "hours.\n"; sub hours_in_day { my $now = 60*60*int( shift()/60/60 ); # Start of this hour my $hour = ( localtime($now) )[2]; $now -= $hour*60*60; # Offset from start of thi +s day $hour = ( localtime($now) )[2]; $hour += 24 unless int($hour/12); return $hour; }

      and gives this output for me (Sunday was the switch from BST to GMT here):

      Today: 24hours. Sun: 25hours.