GuerrillaNtp

GuerrillaNtp is a RFC4330 SNTP/NTP client written in C# that can be embedded in desktop .NET applications to provide them with accurate network time even when the system clock is unsynchronized.

Download

Get GuerrillaNtp from NuGet:

PM> Install-Package GuerrillaNtp -Version 1.4.1

Or clone the source code. Sources and binaries are distributed under Apache License 2.0. The only dependency is .NET Standard 1.5, which should work on every platform.

Tutorial

For the most basic use, instantiate NtpClient with some NTP server (e.g. pool.ntp.org or your private NTP server) and call GetCorrectionOffset() method. You can then use the returned time offset to adjust all time values in your application. Here's a short example:

// query the SNTP server
TimeSpan offset;
try
{
    using (var ntp = new NtpClient(Dns.GetHostAddresses("pool.ntp.org")[0]))
        offset = ntp.GetCorrectionOffset();
}
catch (Exception ex)
{
    // timeout or bad SNTP reply
    offset = TimeSpan.Zero;
}

// use the offset throughout your app
DateTime accurateTime = DateTime.UtcNow + offset;

One instance of NtpClient can be used repeatedly to perform more queries against the same server. You can also call NtpClient.Query() method to gain access to NtpPacket returned by the server. NtpPacket's CorrectionOffset property returns the same value as GetCorrectionOffset(), but NtpPacket has many other useful properties, for example RoundTripTime.

Polling

GuerrillaNtp doesn't have built-in polling mechanism. Polling and multi-server policy is handled by the application. It is your responsibility to be a good netizen and use reasonable polling intervals as well as exponential backoff in case of errors, especially when querying public NTP servers.

Alternatives

GuerrillaNtp is based on Netduino NTP client that I have found on NuGet. I've ported it to desktop .NET Framework and later .NET Standard and added numerous patches. Some of the patches are based on SNTP client from Valer Bocan that has numerous mutations circulating around the Internet. Platform.NET contains fairly competent NTP client, which I have discovered after creating GuerrillaNtp.

Feedback

GuerrillaNtp was developed by Robert Važan with significant contributions from Markus Stöger. If you have any suggestions, report an issue or submit a pull request via bitbucket or github. You can reach the author via email .