Added a little more stability (not really)

This commit is contained in:
watsonb8
2019-11-29 19:27:31 -05:00
parent 85ab39defd
commit ad1fca2009
3 changed files with 29 additions and 59 deletions

View File

@ -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