DaytimeBinarypublic class DaytimeBinary extends Object DaytimeBinary - connect to the Daytime (ascii) service. |
Fields Summary |
---|
public static final short | TIME_PORTThe TCP port for the binary time service. | protected static final long | BASE_DAYSSeconds 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 | MSECConvert from seconds to milliseconds |
Methods Summary |
---|
public static void | main(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 daytime protocol, which uses 4-byte ints.
long remoteTime = (
((long)(is.readUnsignedByte() & 0xff) << 24) |
((long)(is.readUnsignedByte() & 0xff) << 16) |
((long)(is.readUnsignedByte() & 0xff) << 8) |
((long)(is.readUnsignedByte() & 0xff) << 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);
}
|
|