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