UTC server time difference

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

There is a validation on the server that checks when a message was sent from a client and compare it to see the time difference.

This is how I get the timeslot in a client application (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 (USA) end:

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 a UK server, but problems arise when there are different ones.

My questions:

  • Is UTC time accurate on both servers timezone / time independent?
  • Is it time spent on PC system time? (so if the time is set to the wrong one on the system DateTime.UtcNow

    gives me the wrong time?)
+3


source to share


2 answers


Is UTC time accurate on both servers timezone / time independent?

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

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



Yes, MSDN documentation DateTime.UtcNow says the following:

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

+1


source


I am facing one problem which may be strange. In the below state

if ((serverTotalSeconds – requestTotalSeconds) > requestMaxAgeInSeconds)
{
return true;
}

      



For a while my requestTotalSeconds has been more important than serverTotalSeconds. I am using the same code in both applications to create a timestamp.

Please suggest something for this.

0


source







All Articles