1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.net;
17
18 import java.io.IOException;
19 import java.util.Date;
20 import java.io.DataInputStream;
21
22 /***
23 * The TimeTCPClient class is a TCP implementation of a client for the
24 * Time protocol described in RFC 868. To use the class, merely
25 * establish a connection with
26 * {@link org.apache.commons.net.SocketClient#connect connect }
27 * and call either {@link #getTime getTime() } or
28 * {@link #getDate getDate() } to retrieve the time, then
29 * call {@link org.apache.commons.net.SocketClient#disconnect disconnect }
30 * to close the connection properly.
31 * <p>
32 * <p>
33 * @author Daniel F. Savarese
34 * @see TimeUDPClient
35 ***/
36
37 public final class TimeTCPClient extends SocketClient
38 {
39 /*** The default time port. It is set to 37 according to RFC 868. ***/
40 public static final int DEFAULT_PORT = 37;
41
42 /***
43 * The number of seconds between 00:00 1 January 1900 and
44 * 00:00 1 January 1970. This value can be useful for converting
45 * time values to other formats.
46 ***/
47 public static final long SECONDS_1900_TO_1970 = 2208988800L;
48
49 /***
50 * The default TimeTCPClient constructor. It merely sets the default
51 * port to <code> DEFAULT_PORT </code>.
52 ***/
53 public TimeTCPClient ()
54 {
55 setDefaultPort(DEFAULT_PORT);
56 }
57
58 /***
59 * Retrieves the time from the server and returns it. The time
60 * is the number of seconds since 00:00 (midnight) 1 January 1900 GMT,
61 * as specified by RFC 868. This method reads the raw 32-bit big-endian
62 * unsigned integer from the server, converts it to a Java long, and
63 * returns the value.
64 * <p>
65 * The server will have closed the connection at this point, so you should
66 * call
67 * {@link org.apache.commons.net.SocketClient#disconnect disconnect }
68 * after calling this method. To retrieve another time, you must
69 * initiate another connection with
70 * {@link org.apache.commons.net.SocketClient#connect connect }
71 * before calling <code> getTime() </code> again.
72 * <p>
73 * @return The time value retrieved from the server.
74 * @exception IOException If an error occurs while fetching the time.
75 ***/
76 public long getTime() throws IOException
77 {
78 DataInputStream input;
79 input = new DataInputStream(_input_);
80 return (long)(input.readInt() & 0xffffffffL);
81 }
82
83 /***
84 * Retrieves the time from the server and returns a Java Date
85 * containing the time converted to the local timezone.
86 * <p>
87 * The server will have closed the connection at this point, so you should
88 * call
89 * {@link org.apache.commons.net.SocketClient#disconnect disconnect }
90 * after calling this method. To retrieve another time, you must
91 * initiate another connection with
92 * {@link org.apache.commons.net.SocketClient#connect connect }
93 * before calling <code> getDate() </code> again.
94 * <p>
95 * @return A Date value containing the time retrieved from the server
96 * converted to the local timezone.
97 * @exception IOException If an error occurs while fetching the time.
98 ***/
99 public Date getDate() throws IOException
100 {
101 return new Date((getTime() - SECONDS_1900_TO_1970)*1000L);
102 }
103
104 }
105