0 votes
by (150 points)
edited

Hello, I am new about your library, and for my test when I try to retrieve the TimeSpan from NtpResponse I had an "System.OverflowException" with this stack trace:

at Rebex.Net.NtpTimestampDifference.Add(NtpTimestampDifference value) at Rebex.Net.NtpTimestampDifference.op_Addition(NtpTimestampDifference a, NtpTimestampDifference b) at Rebex.Net.NtpResponse.get_TimeOffset()

This is my sample code: ` {

string serverName = Console.ReadLine();

Ntp ntp = new Ntp(serverName)
    {
        VersionNumber = NTP_PROTOCOL_VERSION,
        Timeout = NTP_REQUEST_TIMEOUT_MS
    };

NtpResponse response = ntp.GetTime();

TimeSpan ntpServerDelta = response.TimeOffset.ToTimeSpan();
DateTime currentTime = DateTime.UtcNow + ntpServerDelta;

Console.WriteLine("The server current time is {0} ", currentTime);

Console.ReadLine();

}`

This occurred when I have a big difference between the server and my local machine.

How can I solve this?

Thanks in advance.

by (150 points)
edited

Any idea???

by (144k points)
edited

Hello, sorry for the delay! We are currently looking into this issue. What was the time on your local machine and server, by the way?

by (150 points)
edited

Hello, no problem! My local machine has the current date time (8 October 2013) but the server has very old time something like 10 January 1973. Thanks

by (144k points)
edited

Thanks! It looks like time differences larger than about 34 years (actually new TimeSpan((1L + uint.MaxValue) / 4 * 10000000L) are causing an OverflowException due to a limitation in NtpTimestampDifference structure's minimum/maximum values (+- 78 years).

by (144k points)
edited

Additionally, the NtpTimestamp object limited currently only supports values in 1900 to 2036 range, which means years >2036 also cause issues.

by (144k points)
edited

We will definitely fix both of these issues for the next release and mail you a link to a hotfix when it's ready. Thanks for reporting this, and sorry for inconvenience!

by (150 points)
edited

OK Thanks. In the meanwhile can I use this work around ??:

private static DateTime CalculateCurrentTime(NtpResponse response)
{
    NtpPacket ntpPacket = response.Packet;

    NtpTimestamp serverTimeStamp = ntpPacket.TransmitTimestamp;
    NtpTimestamp localTimeStamp = ntpPacket.DestinationTimestamp;

    NtpTimestampDifference offset = serverTimeStamp - localTimeStamp;

    TimeSpan timeSpan = offset.ToTimeSpan();

    DateTime currentTime = DateTime.UtcNow + timeSpan;
    return currentTime;
}
by (144k points)
edited

Yes, this should work. However, I just sent you a link to a hotfix that shoudl solve this issue. Please give it a try and let me know whether t works!

1 Answer

0 votes
by (144k points)
edited
 
Best answer

This error occured when the time difference between the local and remote time was larger than about 34 years. It has been fixed in Rebex Time 2013 R3.

...