2

I am trying to validate the system time of client’s computer with the actual time (internet time). If for some reason the client’s time settings are not correct or the time and timezone don’t match the local time, I want to notify them to sync the time with their local time in order to use the application. If my question is not clear then this is something that I am trying to mimic, https://knowledge.autodesk.com/support/autocad/troubleshooting/caas/sfdcarticles/sfdcarticles/Incorrect-System-time-warning-when-starting-an-Autodesk-360-application.html

How can I do this time comparison/validation in dart?

smriti
  • 1,073
  • 1
  • 13
  • 25

2 Answers2

3

The main question is IMHO what accuracy you need.

You can just query a NTC and report if there is a discrepancy. If the server is synchronized with such a time server, there shouldn't be a problem.

You can also add an API to your server that returns the server time. Then you read the time from the local system and from the server and check the difference

bool compareTime() {
  var serverTime = await getTimeFromServer(); // not yet existing method to fetch the date and time from the server 
  var clientTime = new DateTime.now().toUtc();
  var diff = serverTime.difference(clientTime).abs();
  if(diff > const Duration(seconds: 5)) {
    print('Time difference too big');
  } else {
    print('Time is fine');
  }
}

Ensure that the time returned from the server is UTC as well, otherwise you're comparing apples with pears.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • assuming my server time is synchronized with NTP server and is correct, how do I get current NTP time? Or did you mean that I would need to fetch the time from server and turn it to UTC and compare? – smriti Jul 31 '17 at 17:58
  • I'm not sure what you mean with your comment. Can you please first tell what accuracy is required (ms, s, m, ...)? – Günter Zöchbauer Jul 31 '17 at 18:03
  • If you want to use the NTP way you would need to fetch the time from a (public) NTP server on the client. I'm not aware of a ready-to-use NTP client implementation and I haven't checked how complicated it would be. – Günter Zöchbauer Jul 31 '17 at 18:05
  • Sorry for not being clear, the accuracy I am looking for is <1sec. yes I was just wondering if there was a ready-to-use NTP client implementation but you cleared that out so I will go and dig into that or go with the other way. – smriti Jul 31 '17 at 18:13
  • I just tried the second way (fetching time from server and comparing it with client time) and it worked very well. thanks for the help! – smriti Jul 31 '17 at 18:41
  • Glad to hear :-) – Günter Zöchbauer Jul 31 '17 at 18:46
0

If you're running server-side, you can shell out to ntpdate -d pool.ntp.org and read the output, parsing the last line. If the time offset is small enough, you're good.

For browser apps, see the StackOverflow at Getting the current GMT world time for a few options.

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70