Mailing List Archive

timelocal questions
I am running Perl 5.001 on Sun OS 4.1.4.2 in a
MDT time zone.
On October 29, 1995, New Mexico went to a MST time zone.

The following perl program expects user to input a start
time and end time and I am using timelocal.pl to figure out the
interval in minutes or hours. Source code follows:-

###################################################################
#!/usr/bin/perl
#

require "timelocal.pl";

# expect input in mm/dd/yy/hh/min format
chop($startday = <STDIN>);
chop($endday = <STDIN>);

# parse dates and convert to system time
# note that range of month is 0-11
($mon,$mday,$year,$hours,$min) = split(/\//,$startday);
print "start month, day, hr and min = $mon, $mday, $hours, $min\n";
$starttime = timelocal(0,$min,$hours,$mday,$mon-1,$year);


($mon,$mday,$year,$hours,$min) = split(/\//,$endday);
print " end month, day, hr and min = $mon, $mday, $hours, $min\n";
$endtime = timelocal(0,$min,$hours,$mday,$mon-1,$year);

$elapsedmin = (($endtime - $starttime) / 60.0);

$elapsedhr = $elapsedmin /60.0;

print "elapsed min = $elapsedmin and hr = $elapsedhr\n";



###################################################################

1) The answer is correct for certain instances and not for other
test cases???


Test cases follow:-

perl test.pl
05/10/95/00/00
10/01/95/00/00
start month, day, hr and min = 05, 10, 00, 00
end month, day, hr and min = 10, 01, 00, 00
elapsed min = 207360 and hr = 3456 ## correct answer

perl test.pl
03/12/95/00/00
04/12/95/00/00
start month, day, hr and min = 03, 12, 00, 00
end month, day, hr and min = 04, 12, 00, 00
elapsed min = 44580 and hr = 743 ## incorrect answer;
## should be 744
## timegm produced
## correct answer

perl test.pl
10/01/95/00/00
10/30/95/00/00
start month, day, hr and min = 10, 01, 00, 00
end month, day, hr and min = 10, 30, 00, 00
elapsed min = 41820 and hr = 697 ## incorrect answer;
## should be 696
## timegm produced
## correct answer

2) It seems like the expected input for hour is from 0 to 23;
And yet timelocal accepts 24 in some instances??

perl test.pl
10/01/95/00/00
10/02/95/24/00
start month, day, hr and min = 10, 01, 00, 00
end month, day, hr and min = 10, 02, 24, 00
elapsed min = 2880 and hr = 48 ## accepts 24 as input;
## produced correct answer


perl test.pl
09/30/95/00/00
10/02/95/24/00
start month, day, hr and min = 09, 30, 00, 00
end month, day, hr and min = 10, 02, 24, 00
Hour out of range 0..23 in timelocal.pl ## does not accept 24;

Any help/suggestions would be greatly appreciated.
Thank you,


Elise Lee, CIC-8
Los Alamos National Laboratory,
MS B272, Los Alamos, NM 87545.
Voice: (505) 667-9652
Fax: (505) 665-6333