FileDocCategorySizeDatePackage
DaytimeBinary.javaAPI DocExample1887Sun Feb 15 20:44:44 GMT 2004None

DaytimeBinary

public class DaytimeBinary extends Object
DaytimeBinary - connect to the standard Time (binary) service.
author
Ian F. Darwin
version
$Id: DaytimeBinary.java,v 1.7 2004/02/16 02:44:43 ian Exp $

Fields Summary
public static final short
TIME_PORT
The TCP port for the binary time service.
protected static final long
BASE_DAYS
Seconds between 1970, the time base for Date(long) and Time. Factors in leap years (up to 2100), hours, minutes, and seconds. Subtract 1 day for 1900, add in 1/2 day for 1969/1970.
public static final long
BASE_DIFF
public static final int
MSEC
Convert from seconds to milliseconds
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] argv)


	     
		String hostName;
		if (argv.length == 0)
			hostName = "localhost";
		else
			hostName = argv[0];

		try {
			Socket sock = new Socket(hostName, TIME_PORT);
			DataInputStream is = new DataInputStream(new 
				BufferedInputStream(sock.getInputStream()));
			// Need to read 4 bytes from the network, unsigned.
			// Do it yourself; there is no readUnsignedInt().
			// Long is 8 bytes on Java, but we are using the
			// existing time protocol, which uses 4-byte ints.
			long remoteTime = (
				((long)(is.readUnsignedByte()) << 24) |
				((long)(is.readUnsignedByte()) << 16) |
				((long)(is.readUnsignedByte()) <<  8) |
				((long)(is.readUnsignedByte()) <<  0));
			System.out.println("Remote time is " + remoteTime);
			System.out.println("BASE_DIFF is " + BASE_DIFF);
			System.out.println("Time diff == " + (remoteTime - BASE_DIFF));
			Date d = new Date((remoteTime - BASE_DIFF) * MSEC);
			System.out.println("Time on " + hostName + " is " + d.toString());
		} catch (IOException e) {
			System.err.println(e);
		}