Package org.apache.commons.net.ntp

# Class TimeStamp

java.lang.Object
org.apache.commons.net.ntp.TimeStamp

All Implemented Interfaces:
  `Serializable`, `Comparable<TimeStamp>`

public class TimeStamp extends Object implements Serializable, Comparable<TimeStamp>
TimeStamp class represents the Network Time Protocol (NTP) timestamp as defined in RFC-1305 and SNTP (RFC-2030). It is represented as a 64-bit unsigned fixed-point number in seconds relative to 0-hour on 1-January-1900. The 32-bit low-order bits are the fractional seconds whose precision is about 200 picoseconds. Assumes overflow date when date passes MAX_LONG and reverts back to 0 is 2036 and not 1900. Test for most significant bit: if MSB=0 then 2036 basis is used otherwise 1900 if MSB=1.

Methods exist to convert NTP timestamps to and from the equivalent Java date representation, which is the number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

See Also:
  - `Date`
  - Serialized Form

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected static final long`
  `msb0baseTime`
  Baseline NTP time if bit-0=0 is 7-Feb-2036 @ 06:28:16 UTC
  `protected static final long`
  `msb1baseTime`
  Baseline NTP time if bit-0=1 is 1-Jan-1900 @ 01:00:00 UTC
  `static final String`
  `NTP_DATE_FORMAT`
  Default NTP date string format.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `TimeStamp(long ntpTime)`
  Constructs a newly allocated NTP timestamp object that represents the native 64-bit long argument.
  `TimeStamp(String hexStamp)`
  Constructs a newly allocated NTP timestamp object that represents the value represented by the string in hexdecimal form (e.g.
  `TimeStamp(Date d)`
  Constructs a newly allocated NTP timestamp object that represents the Java Date argument.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `int`
  `compareTo(TimeStamp anotherTimeStamp)`
  Compares two Timestamps numerically.
  `protected static long`
  `decodeNtpHexString(String hexString)`
  Convert NTP timestamp hexstring (e.g.
  `boolean`
  `equals(Object obj)`
  Compares this object against the specified object.
  `static TimeStamp`
  `getCurrentTime()`
  Gets an NTP timestamp object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.
  `Date`
  `getDate()`
  Gets a Date for an NTP timestamp.
  `long`
  `getFraction()`
  Gets the low-order 32-bits representing the fractional seconds.
  `static TimeStamp`
  `getNtpTime(long dateMillis)`
  Gets an NTP timestamp object from a Java time.
  `long`
  `getSeconds()`
  Gets the high-order 32-bits representing the seconds of this NTP timestamp.
  `long`
  `getTime()`
  Gets a Java standard time in milliseconds from the NTP timestamp.
  `static long`
  `getTime(long ntpTimeValue)`
  Gets a Java standard time from a 64-bit NTP timestamp.
  `int`
  `hashCode()`
  Computes a hash code for this Timestamp.
  `long`
  `ntpValue()`
  Returns the value of this Timestamp as a long value.
  `static TimeStamp`
  `parseNtpString(String s)`
  Parses the string argument as a NTP hexidecimal timestamp representation string (e.g.
  `String`
  `toDateString()`
  Converts this `TimeStamp` object to a `String` of the form:
  `protected static long`
  `toNtpTime(long millis)`
  Converts Java time to 64-bit NTP time representation.
  `String`
  `toString()`
  Converts this `TimeStamp` object to a `String`.
  `static String`
  `toString(long ntpTime)`
  Converts 64-bit NTP timestamp value to a `String`.
  `String`
  `toUTCString()`
  Converts this `TimeStamp` object to a `String` of the form:

  ### Methods inherited from class java.lang.Object

  `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- ## Field Details

  - ### msb0baseTime

    protected static final long msb0baseTime
    Baseline NTP time if bit-0=0 is 7-Feb-2036 @ 06:28:16 UTC
    See Also:
      - Constant Field Values

  - ### msb1baseTime

    protected static final long msb1baseTime
    Baseline NTP time if bit-0=1 is 1-Jan-1900 @ 01:00:00 UTC
    See Also:
      - Constant Field Values

  - ### NTP_DATE_FORMAT

    public static final String NTP_DATE_FORMAT
    Default NTP date string format. E.g. Fri, Sep 12 2003 21:06:23.860. See `java.text.SimpleDateFormat` for code descriptions.
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### TimeStamp

    public TimeStamp(Date d)
    Constructs a newly allocated NTP timestamp object that represents the Java Date argument.
    Parameters:
      `d` - the Date to be represented by the Timestamp object.

  - ### TimeStamp

    public TimeStamp(long ntpTime)
    Constructs a newly allocated NTP timestamp object that represents the native 64-bit long argument.
    Parameters:
      `ntpTime` - the timestamp

  - ### TimeStamp

    public TimeStamp(String hexStamp) throws NumberFormatException
    Constructs a newly allocated NTP timestamp object that represents the value represented by the string in hexdecimal form (e.g. "c1a089bd.fc904f6d").
    Parameters:
      `hexStamp` - the hexadecimal timestamp
    Throws:
      `NumberFormatException` - - if the string does not contain a parsable timestamp.

- ## Method Details

  - ### decodeNtpHexString

    protected static long decodeNtpHexString(String hexString) throws NumberFormatException
    Convert NTP timestamp hexstring (e.g. "c1a089bd.fc904f6d") to the NTP 64-bit unsigned fixed-point number.
    Parameters:
      `hexString` - the string to convert
    Returns:
      NTP 64-bit timestamp value.
    Throws:
      `NumberFormatException` - - if the string does not contain a parsable timestamp.

  - ### getCurrentTime

    public static TimeStamp getCurrentTime()
    Gets an NTP timestamp object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.
    Returns:
      NTP timestamp object set to the current time.
    See Also:
      - `System.currentTimeMillis()`

  - ### getNtpTime

    public static TimeStamp getNtpTime(long dateMillis)
    Gets an NTP timestamp object from a Java time. Note that Java time (milliseconds) by definition has less precision than NTP time (picoseconds) so converting Ntptime to Javatime and back to Ntptime loses precision. For example, Tue, Dec 17 2002 09:07:24.810 is represented by a single Java-based time value of f22cd1fc8a, but its NTP equivalent are all values from c1a9ae1c.cf5c28f5 to c1a9ae1c.cf9db22c.
    Parameters:
      `dateMillis` - the milliseconds since January 1, 1970, 00:00:00 GMT.
    Returns:
      NTP timestamp object at the specified date.

  - ### getTime

    public static long getTime(long ntpTimeValue)
    Gets a Java standard time from a 64-bit NTP timestamp. Note that java time (milliseconds) by definition has less precision than NTP time (picoseconds) so converting NTP timestamp to Java time and back to NTP timestamp loses precision. For example, Tue, Dec 17 2002 09:07:24.810 EST is represented by a single Java-based time value of f22cd1fc8a, but its NTP equivalent are all values ranging from c1a9ae1c.cf5c28f5 to c1a9ae1c.cf9db22c.
    Parameters:
      `ntpTimeValue` - the input time
    Returns:
      the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this NTP timestamp value.

  - ### parseNtpString

    public static TimeStamp parseNtpString(String s) throws NumberFormatException
    Parses the string argument as a NTP hexidecimal timestamp representation string (e.g. "c1a089bd.fc904f6d").
    Parameters:
      `s` - hexstring.
    Returns:
      the Timestamp represented by the argument in hexidecimal.
    Throws:
      `NumberFormatException` - - if the string does not contain a parsable timestamp.

  - ### toNtpTime

    protected static long toNtpTime(long millis)
    Converts Java time to 64-bit NTP time representation.
    Parameters:
      `millis` - Java time
    Returns:
      NTP timestamp representation of Java time value.

  - ### toString

    public static String toString(long ntpTime)
    Converts 64-bit NTP timestamp value to a `String`. The NTP timestamp value is represented as hexadecimal string with seconds separated by fractional seconds by a decimal point; e.g. c1a089bd.fc904f6d == Tue, Dec 10 2002 10:41:49.986
    Parameters:
      `ntpTime` - the 64 bit timestamp
    Returns:
      NTP timestamp 64-bit long value as hexadecimal string with seconds separated by fractional seconds.

  - ### compareTo

    public int compareTo(TimeStamp anotherTimeStamp)
    Compares two Timestamps numerically.
    Specified by:
      `compareTo` in interface `Comparable<TimeStamp>`
    Parameters:
      `anotherTimeStamp` - the `TimeStamp` to be compared.
    Returns:
      the value `0` if the argument TimeStamp is equal to this TimeStamp; a value less than `0` if this TimeStamp is numerically less than the TimeStamp argument; and a value greater than `0` if this TimeStamp is numerically greater than the TimeStamp argument (signed comparison).

  - ### equals

    public boolean equals(Object obj)
    Compares this object against the specified object. The result is `true` if and only if the argument is not `null` and is a `Long` object that contains the same `long` value as this object.
    Overrides:
      `equals` in class `Object`
    Parameters:
      `obj` - the object to compare with.
    Returns:
      `true` if the objects are the same; `false` otherwise.

  - ### getDate

    public Date getDate()
    Gets a Date for an NTP timestamp.
    Returns:
      NTP Timestamp in Java Date

  - ### getFraction

    public long getFraction()
    Gets the low-order 32-bits representing the fractional seconds.
    Returns:
      fractional seconds represented by this NTP timestamp.

  - ### getSeconds

    public long getSeconds()
    Gets the high-order 32-bits representing the seconds of this NTP timestamp.
    Returns:
      seconds represented by this NTP timestamp.

  - ### getTime

    public long getTime()
    Gets a Java standard time in milliseconds from the NTP timestamp.
    Returns:
      the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this NTP timestamp value.

  - ### hashCode

    public int hashCode()
    Computes a hash code for this Timestamp. The result is the exclusive OR of the two halves of the primitive `long` value represented by this `TimeStamp` object. That is, the hash code is the value of the expression:

    >      (int) (this.ntpValue() ^ (this.ntpValue() >>> 32))

    Overrides:
      `hashCode` in class `Object`
    Returns:
      a hash code value for this object.

  - ### ntpValue

    public long ntpValue()
    Returns the value of this Timestamp as a long value.
    Returns:
      the 64-bit long value represented by this object.

  - ### toDateString

    public String toDateString()
    Converts this `TimeStamp` object to a `String` of the form:

    >      EEE, MMM dd yyyy HH:mm:ss.SSS

    See java.text.SimpleDataFormat for code descriptions.
    Returns:
      a string representation of this date.

  - ### toString

    public String toString()
    Converts this `TimeStamp` object to a `String`. The NTP timestamp 64-bit long value is represented as hexadecimal string with seconds separated by fractional seconds by a decimal point; e.g. c1a089bd.fc904f6d == Tue, Dec 10 2002 10:41:49.986
    Overrides:
      `toString` in class `Object`
    Returns:
      NTP timestamp 64-bit long value as hexadecimal string with seconds separated by fractional seconds.

  - ### toUTCString

    public String toUTCString()
    Converts this `TimeStamp` object to a `String` of the form:

    >      EEE, MMM dd yyyy HH:mm:ss.SSS UTC

    See java.text.SimpleDataFormat for code descriptions.
    Returns:
      a string representation of this date in UTC.