Subject: | Daylight Savings or Bug? |
I'm not sure if this is because of daylight savings or something but I
noticed that if I compare the timestamps between 03/11/2007 and
03/12/2007 at 00:00:00, I get a difference of 82800 seconds, not 86400
as I expected. I did notice that 82800/3600 (seconds in an hour) = 23,
which could mean that on that day there are 23 hours instead of 24 due
to daylight savings.
Below is my code:
!/sw/bin/perl
use Time::Local;
if(@ARGV != 2) { &fail; }
$date1_input = shift;
$date2_input = shift;
if($date1_input =~ m|^(\d+)/(\d+)/(\d{4})$|) {
$date1_month=$1-1;
$date1_day=$2;
$date1_year=$3-1900;
}
else { &fail; }
if($date2_input =~ m|^(\d+)/(\d+)/(\d{4})$|) {
$date2_month=$1-1;
$date2_day=$2;
$date2_year=$3-1900;
}
else { &fail; }
$date1_timestamp = timelocal(0,0,0,$date1_day, $date1_month, $date1_year);
$date2_timestamp = timelocal(0,0,0,$date2_day, $date2_month, $date2_year);
print "date1_timestamp = $date1_timestamp\n";
print "date2_timestamp = $date2_timestamp\n";
$diff = $date2_timestamp - $date1_timestamp;
print "date2_timestamp - date1_timestamp = $diff\n";
print "$diff / 86400 = ",$diff/86400,"\n";
sub fail {
die "syntax: localtime.pl <date1> <date2>\nDate syntax:
mm/dd/yyyy\n";
}
Below is the output of the code:
$ perl localtime.pl 03/11/2007 03/12/2007
date1_timestamp = 1173600000
date2_timestamp = 1173682800
date2_timestamp - date1_timestamp = 82800
82800 / 86400 = 0.958333333333333
I then tried using POSIX::mktime and received the results I expected,
86400 seconds between those two dates. Below is that code:
#!/sw/bin/perl
use POSIX;
if(@ARGV != 2) { &fail; }
$date1_input = shift;
$date2_input = shift;
if($date1_input =~ m|^(\d+)/(\d+)/(\d{4})$|) {
$date1_month=$1-1;
$date1_day=$2;
$date1_year=$3-1900;
}
else { &fail; }
if($date2_input =~ m|^(\d+)/(\d+)/(\d{4})$|) {
$date2_month=$1-1;
$date2_day=$2;
$date2_year=$3-1900;
}
else { &fail; }
$date1_timestamp = mktime(0,0,0,$date1_day, $date1_month, $date1_year);
$date2_timestamp = mktime(0,0,0,$date2_day, $date2_month, $date2_year);
print "date1_timestamp = $date1_timestamp\n";
print "date2_timestamp = $date2_timestamp\n";
$diff = $date2_timestamp - $date1_timestamp;
print "date2_timestamp - date1_timestamp = $diff\n";
print "$diff / 86400 = ",$diff/86400,"\n";
sub fail {
die "syntax: posix_time.pl <date1> <date2>\nDate syntax:
mm/dd/yyyy\n";
}
Below is the output of the code:
$ perl posix_time.pl 03/11/2007 03/12/2007
date1_timestamp = 1173600000
date2_timestamp = 1173686400
date2_timestamp - date1_timestamp = 86400
86400 / 86400 = 1