I must be doing something wrong, but I can't figure it out. When I give seconds as an argument to localtime(), I get seconds and 16 hours back.
my $startTime = time;
(process)
my $endTime = time;
my $diffTime = ( $endTime - $startTime );
($sec,$min,$hour) = localtime( $diffTime );
print STDERR "diffTime = $diffTime\n";
print STDERR "hour = $hour\n";
print STDERR "min= $min\n";
print STDERR "sec = $sec\n";
print( sprintf( "Elapsed time : %02d:%02d:%02d\n", $hour, $min, $sec ) );
...always prints:
diffTime = 4
hour = 16
min= 0
sec = 4
Elapsed time : 16:00:04
OKAY. Figured out how to add comments - NoScript settings were too tight.
Thanks...
I can't seem to add comments to this thread, so I'll just thank everyone here.
NOT using gmtime was the problem. It may not be the most efficient solution, but it works for what I need, which is just a simple bit of info for the user to evaluate how long he/she might wait for the routine to complete and make decisions about how large an input dataset he/she is comfortable with.
You want to use the
gmtime
function instead of thelocaltime
function.Since the epoch date is Jan. 1, 1970 00:00:00 GMT/UTC, calling
localtime(0)
will give you the epoch date in your time zone.EDIT: Thanks for the follow-ups. As has been mentioned, this only works if you're measuring an interval that is less than 24 hours. Both the
localtime
andgmtime
functions actually return the values of a date: seconds, minutes, hours, day of the month, month, year, day of the year, and whether the time falls into a daylight savings period. Of course, everything beyond seconds, minutes, and hours don't make sense outside of the context of a date.