LevSelector.com New York
home > Time & Clocks

This page shows how to get exact time (atomic clock) and how to show time on your page.
 
On this page:
main_sites
Navy
Misc
Code (Perl & Java)

 
 Main Sites home - top of the page -

Try these (reload them to see how time changes):
tycho.usno.navy.mil/cgi-bin/timer.pl - my favorite, shows times in different zones
time-a.timefreq.bldrdoc.gov:14/ -
www.infosys.tuwien.ac.at/Services/AtomZeit/GimmeTheTime.pl -
www.worldtimeserver.com/ - shows time in different countries and cities around the globe

Use Javascript to put time on your page:
javascript.internet.com/clocks/index.html -

Browse many links related to time/clocks:
www.panaga.com/clocks/clocks.htm -

www.time.gov/ - Boulder Atomic Clock (a link from nist.gov - The National Institute of Standards and Technology)
www.atomictime.net/ - different ways

 
 Navy home - top of the page -

tycho.usno.navy.mil/ -
You can get text version here: tycho.usno.navy.mil/cgi-bin/timer.pl
(you can parse it as you like to select the time you need).
It is updated every second.
( or you can get a GIF version of Universal Time
   by calling tycho.usno.navy.mil/cgi-bin/utc.gif )

Here is how to convert from Universal Time:
tycho.usno.navy.mil/zones.html
tycho.usno.navy.mil/zones2.html
Same for DAYLIGHT-SAVING TIME:
tycho.usno.navy.mil/zones3.html
tycho.usno.navy.mil/zones4.html
 
More sophisticated example: the clock shows ~25-30 seconds live - and then stops.
ATTENTION: it works only in netscape browser (because Internet Explorer doesn't support this: Content-type:  multipart/x-mixed-replace)
You have to press SHIFT-RELOAD to (re)start this clock.
Clock requires Netscape
To add this clock to your home page, all you have to do is look at the HTML source for this page and copy a reference like this one:

<a href="http://tycho.usno.navy.mil"><img SRC="http://tycho.usno.navy.mil/cgi-bin/nph-usnoclock.gif?zone=EST;ticks=60" ALT="Clock requires Netscape" BORDER=1 height=20 width=113></a>

You can substitute zone (time zone) for one from this list:
UTC - Universal Time
AST - Atlantic Standard/Daylight-Saving Time
EST - Eastern Standard/Daylight-Saving Time (this is the one used in the example)
EET - Eastern Standard Time (no Daylight-Saving Time)
CST - Central Standard/Daylight-Saving Time
MST - Mountain Standard/Daylight-Saving Time
MMT - Mountain Standard Time (no Daylight-Saving Time: Arizona)
PST - Pacific Standard/Daylight-Saving Time
YST - Alaska Standard/Daylight-Saving Time
HST - Hawaii Standard Time
GMT - Greenwich Mean Time/British Summer Time


 
 
 Misc. home - top of the page -



 
 Code home - top of the page -

Example getting time:

Perl:
 

# ------------------------------------------
# read time from time.gov
# ------------------------------------------

use Socket;
use strict;

my $remoteServer = 'www.time.gov';
my $serverTime;
my $proto = getprotobyname('tcp') || 6;
my $port = 8013;
my $serverAddr = (gethostbyname($remoteServer))[4];

# --------------------------
socket(SOCKET, PF_INET, SOCK_STREAM, $proto)
  or die("socket: $!");

my($packFormat) = 'S n a4 x8';   # Windows 95, SunOs 4.1+
#my($packFormat) = 'S n c4 x8';   # SunOs 5.4+ (Solaris 2)
connect(SOCKET, pack($packFormat, AF_INET(), $port, $serverAddr))
  or die("connect: $!");
$serverTime = <SOCKET>;
$serverTime = int ( 0.001 * $serverTime );
close(SOCKET);

# --------------------------
printf("%-20s %8d %s\n", 
  $remoteServer, $serverTime - time, ctime($serverTime));
# --------------------------
printf("%-20s %8s %s\n",
  "localhost", 0, ctime(time()));
# --------------------------

exit();

# ------------------------------------------
sub ctime {
  return(scalar(localtime($_[0])));
}

Java:
 
// select one of the hosts: NIST.GOV or TIME.GOV or BLDRDOC.GOV
int daytimePort = 8013;

// Get serverDateString using this method:

  private void GetServerDateString()
  {
    try
    {
      Socket socket = new Socket(serverName, daytimePort);
      socket.setSoTimeout(3000);
      BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      serverDateString = bufferedreader.readLine();
      socket.close();
      if(serverDateString.equals("error")) {
        serverDateString = "";
        errorBoolean = true;
        return;
      } else { 
        errorBoolean = false; return;
      }
    } // end of try
    catch(IOException _ex) {  serverDateString = "";  }
    errorBoolean = true;
  }

// Then get a long value as number of ms from 1970:
      Long long1 = new Long(serverDateString);
      long l = long1.longValue();