Class SntpConnector

All Implemented Interfaces:
TimeSource<Moment>

public class SntpConnector extends NetTimeConnector<SntpConfiguration>

Connects to a modern time server using the NTP-protocol.

This class needs a socket connection via the port 123. The exact configuration can be set in the constructors but can also be changed at runtime. It is recommended not to connect to the NTP-server during or near a leap second because the NTP-protocol only repeats such a timestamp causing ambivalences.

Note: This implementation is threadsafe.

Since:
2.1
Author:
Meno Hochschild
  • Constructor Details

    • SntpConnector

      public SntpConnector()

      Creates a new instance which is configured by a ServiceLoader.

      Throws:
      IllegalStateException - if no configuration could be found
      See Also:
      ServiceLoader
    • SntpConnector

      public SntpConnector(SntpConfiguration ntc)

      Creates a new instance which is configured by given argument.

      Parameters:
      ntc - SNTP-configuration
    • SntpConnector

      public SntpConnector(String server)

      Creates a new instance which uses a default configuration using the specified NTP4-server.

      Example:

        SntpConnector clock = new SntpConnector("ptbtime1.ptb.de");
        clock.connect();
        System.out.println(clock.currentTime());
       
      Parameters:
      server - NTP4-server
  • Method Details

    • currentTimeInMillis

      public long currentTimeInMillis()

      Returns the current time in milliseconds since the Unix epoch [1970-01-01T00:00:00,000Z].

      UTC leap seconds are never counted.

      Returns:
      count of milliseconds since UNIX-epoch without leap seconds
      Since:
      2.1
    • currentTimeInMicros

      public long currentTimeInMicros()

      Returns the current time in microseconds since the Unix epoch [1970-01-01T00:00:00,000000Z].

      UTC leap seconds are never counted.

      Returns:
      count of microseconds since UNIX-epoch without leap seconds
      Since:
      2.1
    • getLastReply

      public SntpMessage getLastReply()

      Returns the last received message of the NTP-server.

      Returns:
      server message or null if not yet received
      Since:
      2.1
      See Also:
      NetTimeConnector.connect()