Hi all,

I am having some trouble with time functions in Perl that correct their reading for the appropriate timezone. I have minimized my problem into the example script at the bottom of this message, where:
    1. I print a microsecond resolution timestamp for the current time;
2. I read the current timestamp into a variable to be able to subtract it from later readings, effectively zeroing the timestamp; 3. I read the current timestamp and subtract the pointer from (2), then print the resulting timestamp.

I expect that the timestamp (3) will be pretty close to 00:00:00.000000, but rather than that it is an hour off! This has probably to do with the timezone, that is why I included the date command.

===[ The output ]=========
14:00:28.282859
Sat Oct  2 14:00:28 CEST 2010
01:00:00.290085
==========================

Here is the question. What should I do to get a correct and current timestamp (1) until the reference time pointer is set (2) AND from that point onwards timestamps to start from 0:00:00.000000 (3).

For those who are curious what I am trying to accomplish, please refer to my wiki page on a perl script to interface with the Velleman DVM2000 digital multimeter: http://wirespeed.xs4all.nl/mediawiki/index.php/Velleman_DVM2000_PCLink The script continuously prints current time and measured value, and when a SIGUSR1 is issued to the script, the time stamp must be zero'd.

Thanks!


===[ The script ]=========
#!/usr/bin/perl

use warnings;
use strict;

use Time::HiRes qw(gettimeofday);

my ( $seconds, $usec, $sec, $min, $hour, $day, $month, $year );
my $time_pointer = 0;

# (1) Print a current microsecond timestamp
( $seconds, $usec ) = gettimeofday();
( $sec, $min, $hour ) = localtime( $seconds + $usec / 1000000 - $time_pointer );
printf "%02d:%02d:%02d.%06d\n", $hour, $min, $sec, $usec;

# Print current system date and time
print `date`;

# (2) The current time in microsecond resolution
( $seconds, $usec ) = gettimeofday();
$time_pointer = $seconds + $usec / 1000000;

# Print a reset'd microsecond timestamp
( $seconds, $usec ) = gettimeofday();
( $sec, $min, $hour ) = localtime( $seconds + $usec / 1000000 - $time_pointer );
printf "%02d:%02d:%02d.%06d\n", $hour, $min, $sec, $usec;
==========================

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to