Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UTC Time server difference

Tags:

c#

datetime

I have a client and server side application. The client is on UK server and the server is on US one.

There is an validation on the server which check when the message was sent from client and compare it to see the time difference.

This is how I get the time span on client app (UK):

DateTime epochStart = new DateTime(1970, 01, 01, 0, 0, 0, 0, DateTimeKind.Utc);
TimeSpan timeSpan = DateTime.UtcNow - epochStart;
// Convert to string before send it to server 
string requestTimeStamp = Convert.ToUInt64(timeSpan.TotalSeconds).ToString()

On the server (US) end I have:

DateTime epochStart = new DateTime(1970, 01, 01, 0, 0, 0, 0, DateTimeKind.Utc);
TimeSpan currentTs = DateTime.UtcNow - epochStart;

var serverTotalSeconds = Convert.ToUInt64(currentTs.TotalSeconds);
var requestTotalSeconds = Convert.ToUInt64(requestTimeStamp);

if ((serverTotalSeconds - requestTotalSeconds) > requestMaxAgeInSeconds)
{
     //....
}

Everything works fine when the server and client are on UK server but the problems appears when there are on different one.

My questions are:

  1. Is UTC time exact same on both servers no matter the time zone/time difference?
  2. Is the time taken from the PC system time? (so if the time is set to wrong one in the system the DateTime.UtcNow give me wrong time?)
like image 433
Greg Avatar asked Nov 19 '25 13:11

Greg


1 Answers

Is UTC time exact same on both servers no matter the time zone/time difference?

In theory, it should be. However, depending on the NTP server that both client and server talk to they could potentially be different. Hopefully if they are different, it's a very small difference.

Is the time taken from the PC system time? (so if the time is set to wrong one in the system the DateTime.UtcNow give me wrong time?)

Yes, MSDN's DateTime.UtcNow documentation says the following:

Gets a DateTime object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC).

like image 63
Cameron Avatar answered Nov 21 '25 04:11

Cameron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!