Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

NIST Atomic Clock Time

by reptile (Monk)
on May 11, 2000 at 02:50 UTC ( [id://11089]=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info reptile; kjfollet@yahoo.com
Description: Uses LWP::UserAgent to get the current date and time (Eastern) from the NIST Atomic Clock website at www.time.gov This code is public domain.
#!/usr/bin/perl -w

use strict;
use LWP::UserAgent;

my $uri = "http://www.time.gov/timezone.cgi?Eastern/d/-5/java";

my $agent = new LWP::UserAgent;
my $req = HTTP::Request->new(GET => $uri);
my $res = $agent->request($req);

if ( !$res->is_success ) {
    die "HTTP Request Failed: Error ".$res->code;
}

# The line that has the date and time is
# a javascript declaration that looks like
# var NISTSendTime = new Date( "May 10, 2000 22:37:41");
#  Yes, this regex could be done better.
if ( $res->content =~ /NISTSendTime = new Date\(\s*\"(.*?)"\);/ ) {
    print "NIST Date/Time: $1\n";
}
Replies are listed 'Best First'.
RE: NIST Atomic Clock Time
by snowcrash (Friar) on May 15, 2000 at 11:24 UTC
    hmm, since it takes about 4 seconds to fetch the time for me... i dont think i get a very accurate time using a script like this :)
Re: NIST Atomic Clock Time
by jdhedden (Deacon) on Jun 13, 2005 at 18:53 UTC
    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.
Re: NIST Atomic Clock Time
by Anonymous Monk on Nov 21, 2000 at 22:53 UTC
    Uncorrected universal time (ie., time in Greenwich) is returned. Has the Javascript changed? Anyway, a local time correction (e.g., subtract 8 hours for US Pacific) is needed. P. Laub 21-Nov-2000
      A small mod to you code will make it work on eastern time
      if ( $res->content =~ /NISTSendTime = new Date\(\s*\"(.*?)"\);/ ) { print "NIST Date/Time: $1<br>\n"; @words=split(" ",$1); print "GMT ".$words[3]."<br>"; @time=split(":",$words[3]); $gmt=$words[3]; $temp=$gmt-5; if ($temp < 0) {$temp=24-5}; $local=$temp.":".$time[1].":".$time[2]; print "LOCAL ".$local."<br>"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://11089]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-18 14:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found