Some Computer Hints


Perl Script - Daylight Saving Time Check

This script can be used to test and find the exact time your system’s local time will change from normal time to daylight saving time and vice versa. The script takes two parameters: The first one indicates the starting time, the second one indicates the ending time of the test period. Appropriate default values will be used if no parameter is given. Times are in seconds starting from January 1, 1970 (UTC).

#!/usr/bin/perl
# dstcheck.pl - Test daylight saving time changes and configuration.
# Fedon Kadifeli, October 1998 - Mar 2012.

$usage='dstcheck.pl startsec endsec';
$date='08/03/2012 11:55';
$version='0.06';

die "Usage: $usage\n" if $#ARGV != 1 && $#ARGV != -1;
$startsec = $ARGV[0];
$startsec = 733_276_801 if $startsec eq "";
$endsec = $ARGV[1];
$endsec = 2_200_000_000 if $endsec eq "";
@abbr = qw( Sun Mon Tue Wed Thu Fri Sat );

$diff = 9999;
print " Testing seconds from $startsec to $endsec...\n";
for ($t=$startsec; $t<=$endsec; $t+=3600) {
  ($sec1,$min1,$hour1,$mday1,$mon1,$year1,$wday1) = localtime($t);
  ($sec2,$min2,$hour2,$mday2,$mon2,$year2,$wday2) = gmtime($t);
  $diff2 = $hour1 - $hour2;
  $diff2 += 24 if $diff2 < 0;
  if ($diff != $diff2) {
    $diff = $diff2;
    $plus = ($diff >= 0) ? "+" : "";
    $st = sprintf ("%d/%02d/%02d %s %02d:%02d:%02d",
      $year1+1900, $mon1+1, $mday1, $abbr[$wday1], $hour1, $min1, $sec1);
    $st .= sprintf (" [%d/%02d/%02d %s %02d:%02d:%02d UTC]",
      $year2+1900, $mon2+1, $mday2, $abbr[$wday2], $hour2, $min2, $sec2);
    print "$st $plus$diff - Seconds: $t\n";
  }
}

If you run this script using the command:

perl dstcheck.pl 1174784401 2000000000

It should display something like:

 Testing seconds from 1174784401 to 2000000000...
2007/03/25 Sun 04:00:01 [2007/03/25 Sun 01:00:01 UTC] +3 - Seconds: 1174784401
2007/10/28 Sun 03:00:01 [2007/10/28 Sun 01:00:01 UTC] +2 - Seconds: 1193533201
2008/03/30 Sun 04:00:01 [2008/03/30 Sun 01:00:01 UTC] +3 - Seconds: 1206838801
2008/10/26 Sun 03:00:01 [2008/10/26 Sun 01:00:01 UTC] +2 - Seconds: 1224982801
2009/03/29 Sun 04:00:01 [2009/03/29 Sun 01:00:01 UTC] +3 - Seconds: 1238288401
2009/10/25 Sun 03:00:01 [2009/10/25 Sun 01:00:01 UTC] +2 - Seconds: 1256432401
2010/03/28 Sun 04:00:01 [2010/03/28 Sun 01:00:01 UTC] +3 - Seconds: 1269738001
2010/10/31 Sun 03:00:01 [2010/10/31 Sun 01:00:01 UTC] +2 - Seconds: 1288486801
2011/03/28 Mon 04:00:01 [2011/03/28 Mon 01:00:01 UTC] +3 - Seconds: 1301274001
2011/10/30 Sun 03:00:01 [2011/10/30 Sun 01:00:01 UTC] +2 - Seconds: 1319936401
2012/03/25 Sun 04:00:01 [2012/03/25 Sun 01:00:01 UTC] +3 - Seconds: 1332637201
2012/10/28 Sun 03:00:01 [2012/10/28 Sun 01:00:01 UTC] +2 - Seconds: 1351386001
2013/03/31 Sun 04:00:01 [2013/03/31 Sun 01:00:01 UTC] +3 - Seconds: 1364691601
2013/10/27 Sun 03:00:01 [2013/10/27 Sun 01:00:01 UTC] +2 - Seconds: 1382835601
2014/03/31 Mon 04:00:01 [2014/03/31 Mon 01:00:01 UTC] +3 - Seconds: 1396227601
2014/10/26 Sun 03:00:01 [2014/10/26 Sun 01:00:01 UTC] +2 - Seconds: 1414285201
2015/03/29 Sun 04:00:01 [2015/03/29 Sun 01:00:01 UTC] +3 - Seconds: 1427590801
2015/11/08 Sun 03:00:01 [2015/11/08 Sun 01:00:01 UTC] +2 - Seconds: 1446944401
2016/03/27 Sun 04:00:01 [2016/03/27 Sun 01:00:01 UTC] +3 - Seconds: 1459040401

The output depends on the time zone of your system or environment. The above example is for “Europe/Istanbul” (i.e., roughly EET/EEST or GMT+02:00 with DST until the year 2016 and GMT+03:00 with no DST after that). It may be different for your environment. The first column indicates the local time, the second column indicates the GMT (or UTC) time. The third column indicates the difference of your local time from UTC. In the above example, you see that our system switched to UTC+3 at 2016/03/27 Sun 04:00:01 local time and then never back to UTC+2 again.