nagym08
nagym08

Reputation: 75

DateTime.UtcNow from server response shows later time than DateTime.UtcNow called on the client after the response

I have a client application which calls a WEB API via REST. For administrative reasons I want to save the time of the corresponding steps, which looks something like this:

var requestStartTime = DateTime.UtcNow;
response = api.MakeSomeRequest();
serverEntryTime = response.ServerEntryTime;
var requestEndTime = DateTime.UtcNow;

And the server side code looks something like this:

public MyDto MakeSomeRequest()
{
   var dto = new MyDto();
   dto.ServerEntryTime = DateTime.UtcNow;
   //Some action
   return dto;
}

Now, my problem is that when I check the timestamps, sometimes the requestEndTime shows an earlier time (only in milliseconds) than the serverEntryTime:

serverEntryTime = 2021-06-04 12:35:29.123

requestEndTime = 2021-06-04 12:35:29.119

The client and the server are on different machines but in the same timezone. Is it possible that the different DateTime structs on the different machines are operating with this kind of range of error? Or is there some other explanation?

Upvotes: 3

Views: 498

Answers (1)

Z .
Z .

Reputation: 12837

Yes, it is generally expected to have small difference in the times of different machines. You should avoid using the times of the client machines (for consistency).

Upvotes: 4

Related Questions