Divyang Vyas
Divyang Vyas

Reputation: 65

Different time zones for Azure web app

We have a web app running in North UK for which we have explicitly specified a time zone using WEBSITE_TIME_ZONE in App Settings. It's a multi tenant SaaS application with Shared Database architecture. Now we have a new client from US, who is going to use the app. but being in a different timezone it is changing dates and times that are stored in the database.

What should be the best practice to get the app working correctly for different time zones? Should I deploy a separate website for each time zone or should I handle it in code?

Upvotes: 2

Views: 2017

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241475

A few things:

  • The best practice for server-side code, regardless of language or platform, is for it to be time-zone independent. That means not making any hard-coded dependencies on a single time zone, whether that comes from a config file, hosting setting, or server setting.

    • For .NET code, that means you should never use DateTime.Now, DateTimeKind.Local, TimeZoneInfo.Local, and related APIs, because they take their time zone from the server setting.
  • Instead, use APIs that work with UTC, and/or with specific time zones or time zone offsets.

    • For .NET code, that means using things like DateTime.UtcNow, DateTimeOffset, TimeZoneInfo.ConvertTime, TimeZoneInfo.FindSystemTimeZoneById, and several others. Alternatively, consider using Noda Time, which offers a more comprehensive and consistent API.
  • Generally, an application should track the time zone of a user and/or location as a separate field, stored as a string time zone identifier, either in Windows time format, or IANA time zone format. Read the timezone tag wiki for more details (the section titled "Time Zone Databases").

  • Regarding the WEBSITE_TIME_ZONE setting: Only use when all of the following are true:

    • You are hosting an application that uses the system-local time zone. For example, it may get the current time by calling DateTime.Now.
    • All of the users of the application are always in the same time zone.
    • For whatever reason, you cannot make changes to the application to make it time-zone independent.

My personal opinion is that the WEBSITE_TIME_ZONE setting should not exist. There is no need for it in a properly designed application. It is a crutch that should be using sparingly and as a last resort only.

Upvotes: 7

Related Questions