Proper Date and time handling across multiple time zones

A web application or a web service may be located in multiple geographical locations for redundancy or performance reasons. Irrespective of their physical location, the behavior should be consistent from the user’s perspective of view. Consider a situation where a bank customer located on the east coast is trying to open an online account using the bank’s website that is hosted on the west coast. There is a 3 hour time difference between the two locations. If the user is opening an account at 1 am August 2 EST, it is equivalent of 10 pm August 1 PST. If no time difference is considered, then the actual account date creation and recorded date creation would differ.
Web services use Universal Standard Time for Date representation. The UTC Date and time format is independent of time zones. Web services expect Date inputs to be represented in the UTC format. It is up to the developer to ensure that all Date inputs are converted to UTC format before making a web service request call. C# makes this operation trivial because of its rich DateTime data structure.

DateTime todayDate = DateTime.Now.ToUniversalTime();
client.CreateAccount(todayDate);

Opposite operation needs to be performed when web service response is received. The UTC date and time needs to be converted to a local date and time.

DateTime todayDate = client.GetAccountDate();
todayDate = todayDate.ToLocalTime();

Conclusion
The time zone difference is a prime example of challenges that we as developers need to be aware of when developing web application or web services.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.