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