#!/usr/bin/perl # # gettime # # Copyright (C) 2006-2009 # Paul E. Jones # # This program will connect to Packetizer's Time service and # display the current time (UTC time). # use LWP; # Service URL $url = "http://services.packetizer.com/time/?f=text"; # Define a user agent $ua = LWP::UserAgent->new; # Get the page from the server $response = $ua->get($url, "Accept" => "text/plain"); die "Error: Unable to get current time: " . $response->status_line . "\n" unless $response->is_success; die "Error: Invalid content type: " . $response->content_type . "\n" unless $response->content_type eq "text/plain"; # Split the content into an array of lines @content_lines = split('\n', $response->content); # The second line contains the string "TimeString=DDD, MM dd hh:mm:ss yyyy zone" $tag=""; for($i=0; $i<= $#content_lines; $i++) { ($tag, $time_string) = split('=', $content_lines[$i]); if ($tag eq "TimeString") { print "$time_string\n"; last; } } if ($tag ne "TimeString") { die "Error: the time was not returned by the server\n"; }