0

I work on a large project where we need to store the user's time zone information. We are doing so by persisting the 'Id' of the System.TimeZone object as a string (yeah, the id really is a string..) to the database.

Additionally, there is a base seed script that adds an admin user to the database to provide a first user to set things up.

Now my problem is that I set the time zone id in that script to Central European Standard Time, which then doesn't seem to be available on the customers server..

Is it a good idea to store UTC as the time zone for the admin? Is the time zone information 'UTC' always available on a windows system? (like Windows Server 2012 R2)

  • _"permuting the 'Id' of the System.TimeZone object as a string"_ -- what does that mean? When one "permutes" some value "as a string", what is that? As far as storing time zones, since `DateTime.UtcNow` (for example) is available on every system, UTC had _better_ be available on every system. In fact, storing date/time values as UTC is really the best approach; it's too easy to get into trouble with ambiguous values, framework bugs, etc. otherwise. Use UTC internally, convert to a user's preferred time zone as necessary when showing the values (same idea as for other localized data). – Peter Duniho Aug 15 '16 at 06:01
  • @PeterDuniho We don't permute a date time object, we store the TimeZone of the user. It seems a bit strange first, but there is a TimeZone 'UTC', which represents the TimeZone 'Greenwich Standard Time'. We store the 'Id' property of the System.TimeZone object, in my case, it's 'UTC'. – Dominic Sauder Aug 15 '16 at 06:16
  • _"We don't permute a date time object"_ -- never said you did. But _you_ wrote that you _do_ "permute the 'Id' of the System.TimeZone object"_. It's not clear at all what that means. – Peter Duniho Aug 15 '16 at 06:17
  • Sorry for my bad english, by saying 'permuting' i meant storing somthing in a database. I found out that the word that i use in my language (german) has quite a different meaning in english.. – Dominic Sauder Aug 15 '16 at 06:20
  • 2
    Try "persisting" or "serializing" (the latter being a better, more specific technical term). (By the way, it's true that Greenwich Mean Time (GMT) or Greenwich Standard Time is for most intents and purposes equivalent to UTC (Coordinated Universal Time), but technically the two are not precisely the same.) – Peter Duniho Aug 15 '16 at 06:21
  • Is there a specific reason for storing that information? If you can retrieve that information from the system, why do you want to save it? Just get it from the system when needed – Sir Rufo Aug 15 '16 at 06:32
  • No, that's surely a bad practice. The .NET Framework does not rely on the machine having a "UTC" timezone, it always intercepts the string and uses hard-coded timezone info. It is in general a drastically bad practice, selecting the timezone is part of the Windows install procedure and overriding the admin's choice is never a good idea. If you want to do this anyway then at least stick with what you have, it is a smoke detector for a screwed up machine :) – Hans Passant Aug 15 '16 at 07:39

1 Answers1

0

A few things:

  • I think you meant the System.TimeZoneInfo class, because the System.TimeZone class does not have an Id property. That's fine, because you should consider System.TimeZone deprecated anyway.

  • The time zones exposed by the TimeZoneInfo class are the standard Windows operating system time zones, found in the registry under the following key:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones
    
  • Yes, the "UTC" time zone is available on every Windows system, and so should the "Central European Standard Time" time zone. If they are not available on the customer's Windows computers, then their Windows registry has been manipulated manually, or become corrupted. It can be restored by taking a backup of the registry key shown above from another computer and copying it in place, or by simply installing the latest updated listed here. (The June 2016 update, as of writing this.)

  • Storing the Id of the time zone is perfectly fine. Yes, it is a string. That's how time zone identifiers work. You should read the time zone tag wiki for better understanding.

  • Nobody can answer which time zone your admin should use. That's entirely specific to your application. Perhaps there could be multiple admins for different parts of the world, or perhaps admins might need to see some data in context using some other time zone. There's no one right approach here. You may be able to get some ideas from reading Daylight saving time and time zone best practices.

  • Time zone issues can be tricky, and there are many good questions here on StackOverflow you should research before going much further. You might also want to read the many articles on Wikipedia about time zones in general or in particular locations, or look for training materials such as videos on Pluralsight.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575