001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.net.time;
019    
020    import java.io.DataInputStream;
021    import java.io.IOException;
022    import java.util.Date;
023    
024    import org.apache.commons.net.SocketClient;
025    
026    /***
027     * The TimeTCPClient class is a TCP implementation of a client for the
028     * Time protocol described in RFC 868.  To use the class, merely
029     * establish a connection with
030     * {@link org.apache.commons.net.SocketClient#connect  connect }
031     * and call either {@link #getTime  getTime() } or
032     * {@link #getDate  getDate() } to retrieve the time, then
033     * call {@link org.apache.commons.net.SocketClient#disconnect  disconnect }
034     * to close the connection properly.
035     * <p>
036     * <p>
037     * @see TimeUDPClient
038     ***/
039    
040    public final class TimeTCPClient extends SocketClient
041    {
042        /*** The default time port.  It is set to 37 according to RFC 868. ***/
043        public static final int DEFAULT_PORT = 37;
044    
045        /***
046         * The number of seconds between 00:00 1 January 1900 and
047         * 00:00 1 January 1970.  This value can be useful for converting
048         * time values to other formats.
049         ***/
050        public static final long SECONDS_1900_TO_1970 = 2208988800L;
051    
052        /***
053         * The default TimeTCPClient constructor.  It merely sets the default
054         * port to <code> DEFAULT_PORT </code>.
055         ***/
056        public TimeTCPClient ()
057        {
058            setDefaultPort(DEFAULT_PORT);
059        }
060    
061        /***
062         * Retrieves the time from the server and returns it.  The time
063         * is the number of seconds since 00:00 (midnight) 1 January 1900 GMT,
064         * as specified by RFC 868.  This method reads the raw 32-bit big-endian
065         * unsigned integer from the server, converts it to a Java long, and
066         * returns the value.
067         * <p>
068         * The server will have closed the connection at this point, so you should
069         * call
070         * {@link org.apache.commons.net.SocketClient#disconnect  disconnect }
071         * after calling this method.  To retrieve another time, you must
072         * initiate another connection with
073         * {@link org.apache.commons.net.SocketClient#connect  connect }
074         * before calling <code> getTime() </code> again.
075         * <p>
076         * @return The time value retrieved from the server.
077         * @exception IOException  If an error occurs while fetching the time.
078         ***/
079        public long getTime() throws IOException
080        {
081            DataInputStream input;
082            input = new DataInputStream(_input_);
083            return (input.readInt() & 0xffffffffL);
084        }
085    
086        /***
087         * Retrieves the time from the server and returns a Java Date
088         * containing the time converted to the local timezone.
089         * <p>
090         * The server will have closed the connection at this point, so you should
091         * call
092         * {@link org.apache.commons.net.SocketClient#disconnect  disconnect }
093         * after calling this method.  To retrieve another time, you must
094         * initiate another connection with
095         * {@link org.apache.commons.net.SocketClient#connect  connect }
096         * before calling <code> getDate() </code> again.
097         * <p>
098         * @return A Date value containing the time retrieved from the server
099         *     converted to the local timezone.
100         * @exception IOException  If an error occurs while fetching the time.
101         ***/
102        public Date getDate() throws IOException
103        {
104            return new Date((getTime() - SECONDS_1900_TO_1970)*1000L);
105        }
106    
107    }
108