NtpResponse Exception

0 votes
asked Oct 4, 2013 by LAMeg (150 points)
edited Dec 6, 2013

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.

commented Oct 8, 2013 by LAMeg (150 points)
edited Oct 8, 2013

Any idea???

commented Oct 8, 2013 by Lukas Pokorny (85,170 points)
edited Oct 8, 2013

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?

commented Oct 8, 2013 by LAMeg (150 points)
edited Oct 8, 2013

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

commented Oct 8, 2013 by Lukas Pokorny (85,170 points)
edited Oct 8, 2013

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).

commented Oct 8, 2013 by Lukas Pokorny (85,170 points)
edited Oct 8, 2013

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

commented Oct 8, 2013 by Lukas Pokorny (85,170 points)
edited Oct 8, 2013

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!

commented Oct 8, 2013 by LAMeg (150 points)
edited Oct 8, 2013

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;
}
commented Oct 8, 2013 by Lukas Pokorny (85,170 points)
edited Oct 8, 2013

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
answered Dec 6, 2013 by Lukas Pokorny (85,170 points)
edited Dec 6, 2013
 
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.

...