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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.