Alex Rivera | Logout

Storing a users Time Zone preference

Asked 2012-09-01T21:08:00.317
9

My requirement here is fairly standard; I need to allow users to select their current TimeZone and save it against their account. I will then use this value to to convert stored DateTime values to a localised time.

My current thinking is that I will allow users to select a .NET TimeZoneInfo.Id from a list (held in the DB to allow more friendly descriptions but constructed using TimeZoneInfo.GetSystemTimeZones()) - I will then use this value to return the relevant TimeZoneInfo instance using TimeZoneInfo.FindSystemTimeZoneById() and perform the conversion. The main problem I see here is that TimeZoneInfo.Id is a value held in the registry and is not a "standard" Id (compared to Olson for example). As such, it is possible that server updates/migrations may invalidate the stored Ids completely and break the conversions..

In short - is this approach valid/safe? If not, is there a better way of storing a users timezone preference while also handling daylight savings etc without tons of additional logic?

Edit
Report

1 Answer

10

I just found this old-ish unanswered question, so thought I should take a stab at it.

In short - is this approach valid/safe?

It all depends on what you plan to do with them.

If you are just using it in server-side code, then yes. You simply store the Id of the timezone, and show the user the corresponding DisplayName. FindSystemTimeZoneById and GetSystemTimeZones are perfectly valid for this.

Keep in mind that while the Id values are always the same - the DisplayName properties will be different based on the language of the Windows operating system that you are running the code on. The implementation is not culture aware, so just setting a different target culture in .Net will not change the DisplayName string.

The records in the Microsoft Windows time zone database are reasonably stable, and are kept updated via Windows Update. Some of the information comes from the registry, but the localized resource strings come from tzres.dll. Of course, all of that is hidden from you by TimeZoneInfo and related classes.

However, if you are passing these time zone Id values to other systems - watch out. There are some variations with prior versions of Windows. For example, I know that the display names used to all say "GMT" in them, and now more correctly say "UTC". The Id values are the same, but who knows exactly what else is not consistent. Especially if the target computer hasn't received the same set of Windows Updates that you have. By the way - the updates are announced here.

You should also know a few things about the Windows time zones database:

  • It is incapable of representing more than two DST transitions per calendar

answered 2013-05-21T21:42:32.517

Your Answer