1

I'm making a Booking Web App that is highly depend on server time. I want to be sure the time i get in my app using System.DateTime.Now would be real time.

Is it possible that somehow server time or UTC Time Zone changes in an unwanted manner?

Mohammad Barbast
  • 1,753
  • 3
  • 17
  • 28
  • 1
    "Is it possible that somehow server time or UTC Time Zone changes in an unwanted manner?". Yes, it is possible. A solution is to periodically check the server time by getting the exact time from a NTP server. –  Dec 06 '19 at 16:18
  • 1
    I mean, you can [get the current time from an online service](https://stackoverflow.com/questions/6435099/how-to-get-datetime-from-the-internet), but that's a really bad idea. You should just trust the server's current time. Computers already do the time syncing for you with most out-of-the-box setups. Time drift shouldn't be an issue – gunr2171 Dec 06 '19 at 16:18
  • @gunr2171 Why it is a bad idea? –  Dec 06 '19 at 16:19
  • 2
    @ElmoDev001 in my opinion, the application should just rely on the server's time, which the host OS will already do the NTP sync. It just adds extra overhead to the app, and "rolling your own" for this sort of thing isn't good. – gunr2171 Dec 06 '19 at 16:21
  • 1
    I partially agree with you, but it has already happened to me to have a wrong time on a virtual machine due to a vmware bug. –  Dec 06 '19 at 16:24
  • @ElmoDev001 that vmware bug (because I'm curious) was it about having the wrong time zone or being minutes off from the host system? – gunr2171 Dec 06 '19 at 16:27
  • About 6-7 hours off from the host system. –  Dec 06 '19 at 16:29
  • @ElmoDev001 if there was any such bug, and not some gross misconfiguration of the guest VM, you can be sure people in *banks* would have found out over 10 years ago. VMWare was used by everyone to host VM servers around 2010. Servers don't sync with the host. They sync with the domain controller, or Kerberos server, or whatever is used for the entire network to issue tokens. Even *minutes* out of sync would mean all connections to/from the affected server would be rejected – Panagiotis Kanavos Dec 06 '19 at 16:39
  • I am not an expert of VMware, I am the developer of a cloud service that, among other things, send the correct time to all the connected devices (intrusion control panels) once a day. Few years ago we sent to all the devices the wrong time (6-7 hours off) because the time of the server was wrong. Our IT manager said that it was due to a VMware issue. After that I added to the application a time check with public NTP servers, once a day, and if the server time is wrong (10 seconds off) the application DON'T send the current time to the devices. That's all. –  Dec 09 '19 at 07:38

2 Answers2

2

You can literally set the server time to GMT/UTC. Of course, that doesn't stop someone else from changing it later and borking your app without knowing.

In general, you should not use DateTime.Now ever. Honestly, you shouldn't even use DateTime, if time is of importance to you. DateTimeOffset is more appropriate in almost all cases, and in particular you should use DateTimeOffset.UtcNow. Though, you'll at least have less problems with DateTimeOffset.Now than DateTime.Now, because at least the server's offset will come into play in calculations.

If you need precise time handling, then you should use Noda Time, a library maintained by Jon Skeet, specifically because none of the .NET CLR date/time handling is 100% accurate.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • (Nothing wrong with this post) I think there's two competing ideas for "wrong time" going on here. This post is addressing the "host OS has the right time, just the wrong time zone set". The other option is what Elmo is saying, that the time is _compeletly_ wrong, irregardless of the time zone. – gunr2171 Dec 06 '19 at 16:32
  • @gunr2171 gross misconfiguration of the VM guest isn't a software problem. Banks use VMWare extensively for virtual servers. The sync with the domain controllers, not the host. If they didn't, even a simple connection would fail because the Kerberos tickets would get rejected due to invalid time. Any VM server with such a difference would become invisible to the entire bank - and trigger an immediate response from IT – Panagiotis Kanavos Dec 06 '19 at 16:37
  • @PanagiotisKanavos "gross misconfiguration of the VM guest isn't a software problem" completely agree. It's not the software's job to make sure the OS is correct. – gunr2171 Dec 06 '19 at 16:40
  • Why not? I believe that a robust application should also handle "unlikely, but not impossible" cases. It is the same concept as the crc of a file read from the disk: it should not be the software's job to verify that the disk is not damaged. –  Dec 09 '19 at 10:13
1

Is it possible that somehow server time or UTC Time Zone changes in an unwanted manner?

Yes, it is possible. A solution is to periodically check the server time by getting the exact time from a NTP server.

Example: How to Query an NTP Server using C#?

You can list a pool of NTP servers in you application in order to have a bit of redundancy.

  • 2
    Using NTP is the job of the server and the OS, not the application. Even VM guests use NTP to get the time from *the domain controller* in a domain, not the host. If the OS time wasn't trustworthy, network connections, HTTPS, certificates, Kerberos tickets, actually *everything* in a corporate network, wouldn't work – Panagiotis Kanavos Dec 06 '19 at 16:35
  • 3
    Besides, every NTP server will return a *different* time, and *all* of the responses will be wrong due to network latencies. The OS uses specific algorithms to estimate that latency and correct it. The application would have to execute the same algorithms. – Panagiotis Kanavos Dec 06 '19 at 16:44