1

There is this API that when called gives me the time of a remote server. This is the response JSON of that API:

{
    hour: 12,
    minute: 2,
    second: 20,
    msFormat: "/Date(1590478340707)/"
}

This time is not in sync with the server my code runs on. And I need milliseconds precision for my code according to time of the remote server.

I want to call another API of that server, exactly based on some time relative to that server. Thus I can't use DateTime.Now of my own server, because they are not in sync. My server and the remote server have the same year, same month, same day, but they might sometimes even be off by hours.

I saw this question, but answers assume that I have access to JavaScript code on that server to get time. I don't. I also saw this question, but it's about sending my own client's time to my server. And as I said, I don't have access to creation of JavaScript's datetime. I only have access to data shown above.

Now I need to:

  1. Parse this time, exactly as it is, down to milliseconds precision
  2. Tick based on this time, so that I can do something precisely at some point in time, relative to it

I wanted to try new DateTime(year, month, day, hour, minute, second, millisecond), but I don't have the number of milliseconds (it's in the last parameter called msFormat and I don't know it until I extract it, which is what I don't know).

I tried new DateTime(1970, 01, 01).AddMilliseconds(1590478340707) which gives me a wrong time, because I know it's off even based on the remote server's time.

I also tried new System.Timers.Timer(1).Elapsed += HandleTimeBasedOnRemoteServerTime;. But now I have no idea how to know the time of that server, because I can't use DateTime.Now.

Could you please help?

  • Use of `StopWatch`? [Link](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.stopwatch?view=netcore-3.1) – lupaulus May 26 '20 at 07:50
  • _"I need milliseconds precision"_ - there is such a thing as network latency, so I guess you can't be so precise. Maybe [NTP](https://en.wikipedia.org/wiki/Network_Time_Protocol) might help you? – vasily.sib May 26 '20 at 07:57
  • `/Date(1590478340707)/` is date time format it JSON, but it doesn't match the hour/minutes/second value – Pavel Anikhouski May 26 '20 at 08:01

1 Answers1

1

The best solution is to ensure both servers are synchronising their clocks via ntp or similar protocol, making this someone else's problem, so you can simply assume the clock is correct.

If you can't do that for some reason, it should be possible to re-use ntp's source code as a library and construct fake messages from the data you have available. But with so many layers of abstraction, the accuracy of your time estimate may never reach 1ms.

My server and the remote server have the same year, same month, same day, but they might sometimes even be off by hours.

Are you sure the servers aren't just in different timezones? Always ensure that you are calculating times as UTC, even if you have to display / edit values in local timezones. For this, DateTimeOffset is a much better datatype as it can correctly preserve the source timezone information.

Back to your failed examples;

new DateTime(1970, 01, 01)

That constructor will leave the Kind unspecified, when you should be explicit about using UTC. It would be simpler to use the ticks constructor;

new DateTime(1590478340707 * 1000, DateTimeKind.Utc);
Jeremy Lakeman
  • 9,515
  • 25
  • 29