Added a little more stability (not really)
This commit is contained in:
@ -1,13 +1,27 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Aurora.Utils
|
||||
{
|
||||
public class Time
|
||||
{
|
||||
public Time(DateTime dateTime, TimeSpan elapsed)
|
||||
{
|
||||
this.DateTime = dateTime;
|
||||
this.Elapsed = elapsed;
|
||||
}
|
||||
|
||||
public DateTime DateTime { get; private set; }
|
||||
public TimeSpan Elapsed { get; private set; }
|
||||
}
|
||||
public static class TimeUtils
|
||||
{
|
||||
public static DateTime GetNetworkTime()
|
||||
public static Time GetNetworkTime()
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
|
||||
//default Windows time server
|
||||
const string ntpServer = "time.windows.com";
|
||||
|
||||
@ -29,7 +43,7 @@ namespace Aurora.Utils
|
||||
|
||||
//Stops code hang if NTP is blocked
|
||||
socket.ReceiveTimeout = 3000;
|
||||
|
||||
stopwatch.Start();
|
||||
socket.Send(ntpData);
|
||||
socket.Receive(ntpData);
|
||||
socket.Close();
|
||||
@ -53,8 +67,9 @@ namespace Aurora.Utils
|
||||
|
||||
//**UTC** time
|
||||
var networkDateTime = (new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddMilliseconds((long)milliseconds);
|
||||
stopwatch.Stop();
|
||||
|
||||
return networkDateTime.ToLocalTime();
|
||||
return new Time(networkDateTime.ToLocalTime(), stopwatch.Elapsed); ;
|
||||
}
|
||||
|
||||
// stackoverflow.com/a/3294698/162671
|
||||
|
Reference in New Issue
Block a user