Mohammad Barbast
Mohammad Barbast

Reputation: 2049

How do i make sure that server time is real time?

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?

Upvotes: 1

Views: 2104

Answers (2)

Chris Pratt
Chris Pratt

Reputation: 239250

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.

Upvotes: 2

user4763353
user4763353

Reputation:

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.

Upvotes: 1

Related Questions