Class NtpTime
Provides NTP (Network Time Protocol) time synchronization and conversion utilities.
public static class NtpTime
- Inheritance
-
NtpTime
- Inherited Members
Remarks
This class maintains a synchronized clock offset from network time servers and provides methods to convert between DateTime and NTP timestamp formats.
Properties
Correction
Gets the current NTP time correction value in ticks.
public static long Correction { get; }
Property Value
Remarks
Returns 0 if synchronization has not been performed yet.
LastSyncedAt
Gets the media time value at which the last synchronization occurred.
public static long LastSyncedAt { get; }
Property Value
LastSyncedNtpTime
Gets the NTP timestamp of the last synchronization.
public static ulong LastSyncedNtpTime { get; }
Property Value
LocalNtpServer
Gets or sets the NTP server address in the local network.
public static string LocalNtpServer { get; set; }
Property Value
Remarks
This server is used as a fallback when OnlineMode is false
and public NTP server synchronization fails 10 times in a row.
OnlineMode
Gets or sets a value indicating whether the application is online and can access NTP servers.
public static bool OnlineMode { get; set; }
Property Value
Remarks
If true, NtpTime will keep trying to synchronize with NTP servers even if it fails.
If false, NtpTime will stop attempting synchronization with public NTP servers after 10 consecutive failures.
The default value is true.
Methods
FromNtpTime(ulong)
Converts an NTP timestamp to a DateTime value.
public static DateTime FromNtpTime(ulong ntpTime)
Parameters
ntpTimeulongThe NTP 64-bit timestamp to convert.
Returns
- DateTime
The equivalent UTC DateTime value.
Now()
Gets the current time as an NTP timestamp.
public static ulong Now()
Returns
- ulong
The current time in NTP 64-bit timestamp format.
Remarks
This method returns the synchronized NTP time if available, or the local UTC time if not synchronized. Note: This function only operates correctly in the 1900-2036 primary epoch defined by NTP.
NowAsUtcDateTime()
Gets the current synchronized time as a UTC DateTime.
public static DateTime NowAsUtcDateTime()
Returns
- DateTime
The current synchronized time, or the local UTC time if not synchronized.
Sync()
Synchronizes the local time with network time servers asynchronously.
public static Task<bool> Sync()
Returns
Remarks
This method performs multiple NTP queries and averages the results to obtain an accurate time offset. Synchronization is skipped if it was performed less than 10 minutes ago.
SyncManual(long)
Synchronizes NTP time using the provided correction value.
public static void SyncManual(long correction)
Parameters
correctionlongThe correction value in ticks to apply to the local time.
Remarks
This method can be used to perfectly synchronize processes running on the same computer by sharing the correction value obtained from a previous synchronization.
ToNtpTime(DateTime)
Converts a DateTime value to an NTP timestamp.
public static ulong ToNtpTime(DateTime dt)
Parameters
dtDateTimeThe DateTime value to convert.
Returns
- ulong
The equivalent NTP 64-bit timestamp.
ToNtpTime(long)
Converts a tick value to an NTP timestamp.
public static ulong ToNtpTime(long ticks)
Parameters
tickslongThe tick value representing a UTC DateTime.
Returns
- ulong
The equivalent NTP 64-bit timestamp.