Supported Time Protocols in the NetClock Component
The NetClock component retrieves the current time from a network time server, which allows applications to check or synchronize the time of the local system. It supports two protocols, which are selected using the Protocol property:
| Protocol | Value | Default Port | Description |
|---|---|---|---|
| SNTP | tpSNTP (default) | 123 | The Simple Network Time Protocol (RFC 4330), which is a simplified form of the Network Time Protocol (NTP) used by most time servers today. |
| Time Protocol | tpTime | 37 | The Time Protocol (RFC 868), which returns the time as the number of seconds since January 1, 1900. This protocol is obsolete, and few public servers still support it. |
Both protocols use UDP. When the Protocol property is changed, the component automatically sets the TimePort property to the default port of the selected protocol.
Retrieving the Time
By default, the component uses SNTP to request the time from pool.ntp.org. To retrieve the time, set the TimeServer property (if a different server should be used), and call the GetTime method. The time returned by the server is then available in the ServerTime property (in GMT), and the equivalent local time is available in the LocalTime property.
NetClock netclock = new NetClock();
netclock.TimeServer = "time.google.com";
netclock.GetTime();
// In the .NET edition, both values use the format "MM/dd/yyyy HH:mm:ss" (24-hour time).
Console.WriteLine("Server time (GMT): " + netclock.ServerTime);
Console.WriteLine("Local time: " + netclock.LocalTime);
To use the Time Protocol instead, set the Protocol property to tpTime before calling GetTime, and set TimeServer to a server that supports it.
netclock.Protocol = NetClockProtocols.tpTime; // TimePort is set to 37 automatically.
netclock.TimeServer = "time.example.com";
netclock.GetTime();
Setting the System Clock
The GetAndSetTime method retrieves the time in the same way as GetTime, and then sets the system clock to the received time.
netclock.GetAndSetTime();
Setting the system clock requires the appropriate permissions (e.g., on Windows the application must run with administrator privileges), and this method is not available on all platforms and editions. If the clock cannot be set, the method throws an exception.
Notes
- NetClock sends a single request to the server and uses the time in the reply, without compensating for network delay. The result is therefore accurate to within the time it takes the request to reach the server and the reply to return (typically a fraction of a second). To keep a system's clock continuously synchronized, use the operating system's time service (such as Windows Time or chronyd) instead.
- Since UDP does not guarantee delivery, the request may not receive a reply (e.g., when a firewall blocks the port). In this case, GetTime throws a timeout error after the number of seconds specified by the Timeout property.
- The format of ServerTime and LocalTime can differ between editions, so check the documentation for the edition in use before parsing these values.
- Public SNTP servers include pool.ntp.org (the default), time.google.com, time.windows.com, and time.nist.gov.
Note that early versions of the NetClock component used the Time Protocol by default. In current versions, SNTP is the default.
We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.