Using the 'non-java' option for the CGI, the time is reported properly for the timezone. Further, this option results in less network traffic (5K as opposed to 20K), and hence a slightly quicker response. The code below also checks the response against the local (computer) time so you can tell if you're in sync with the remote time server. (If you have a slow internet connection, this may app may not work for you because it won't get through the loop's conditional.)
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
# Modify the args as needed for your locale
my $URI = 'http://www.time.gov/timezone.cgi?Eastern/d/-5';
MAIN:
{
# Create user agent
my $ua = LWP::UserAgent->new;
# Create request
my $req = HTTP::Request->new(GET => $URI);
# Get remote time, and monitor local (computer) time
my ($res, $t1, $t2);
do {
# Get local time
$t1 = time();
# Execute request
$res = $ua->request($req);
# Read local time again
$t2 = time();
# Loop if the two measures of local time differ
} while ($t1 != $t2);
# Check response
if ($res->is_success) {
# Extract remote time
my ($hh, $mm, $ss) = $res->content =~ />(\d{1,2}):(\d{2}):(\d{
+2})</;
if (defined($ss)) {
# Output remote time
printf("Remote time: %02d:%02d:%02d\n", $hh, $mm, $ss);
# If local and remote times differ, then output local time
+ as well
if ($ss != $t1 % 60) {
($ss, $mm, $hh) = (localtime($t1))[0-2];
printf("Local time : %02d:%02d:%02d\n", $hh, $mm, $ss)
+;
}
} else {
print("ERROR: Time not found in returned results.\n");
}
} else {
print('ERROR: ', $res->status_line, "\n");
}
}
exit(0);
# EOF
Remember: There's always one more bug.
|