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
018package org.apache.commons.net.time;
019
020import java.io.DataInputStream;
021import java.io.IOException;
022import java.util.Date;
023
024import 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 * @author Daniel F. Savarese
038 * @see TimeUDPClient
039 ***/
040
041public final class TimeTCPClient extends SocketClient
042{
043    /*** The default time port.  It is set to 37 according to RFC 868. ***/
044    public static final int DEFAULT_PORT = 37;
045
046    /***
047     * The number of seconds between 00:00 1 January 1900 and
048     * 00:00 1 January 1970.  This value can be useful for converting
049     * time values to other formats.
050     ***/
051    public static final long SECONDS_1900_TO_1970 = 2208988800L;
052
053    /***
054     * The default TimeTCPClient constructor.  It merely sets the default
055     * port to <code> DEFAULT_PORT </code>.
056     ***/
057    public TimeTCPClient ()
058    {
059        setDefaultPort(DEFAULT_PORT);
060    }
061
062    /***
063     * Retrieves the time from the server and returns it.  The time
064     * is the number of seconds since 00:00 (midnight) 1 January 1900 GMT,
065     * as specified by RFC 868.  This method reads the raw 32-bit big-endian
066     * unsigned integer from the server, converts it to a Java long, and
067     * returns the value.
068     * <p>
069     * The server will have closed the connection at this point, so you should
070     * call
071     * {@link org.apache.commons.net.SocketClient#disconnect  disconnect }
072     * after calling this method.  To retrieve another time, you must
073     * initiate another connection with
074     * {@link org.apache.commons.net.SocketClient#connect  connect }
075     * before calling <code> getTime() </code> again.
076     * <p>
077     * @return The time value retrieved from the server.
078     * @exception IOException  If an error occurs while fetching the time.
079     ***/
080    public long getTime() throws IOException
081    {
082        DataInputStream input;
083        input = new DataInputStream(_input_);
084        return (input.readInt() & 0xffffffffL);
085    }
086
087    /***
088     * Retrieves the time from the server and returns a Java Date
089     * containing the time converted to the local timezone.
090     * <p>
091     * The server will have closed the connection at this point, so you should
092     * call
093     * {@link org.apache.commons.net.SocketClient#disconnect  disconnect }
094     * after calling this method.  To retrieve another time, you must
095     * initiate another connection with
096     * {@link org.apache.commons.net.SocketClient#connect  connect }
097     * before calling <code> getDate() </code> again.
098     * <p>
099     * @return A Date value containing the time retrieved from the server
100     *     converted to the local timezone.
101     * @exception IOException  If an error occurs while fetching the time.
102     ***/
103    public Date getDate() throws IOException
104    {
105        return new Date((getTime() - SECONDS_1900_TO_1970)*1000L);
106    }
107
108}
109