Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimeZoneInfo.GetSystemTimeZones() returns inconsistent information on different computers

Tags:

timezone

c#

If I use

foreach (TimeZoneInfo info in TimeZoneInfo.GetSystemTimeZones())
{
    if (info.ToString()...

to acquire a list of timezone strings, then on some systems it returns strings prefixed with (GMT) and on other systems with (UTC). Not 100% sure about this, but it seems Vista lists (GMT) values, and Windows 7 lists (UTC) values, and Windows Server lists (GMT)

What is going on, and can I get Windows 7 to list the Vista GMT values? I want a consistent list that will work across all Windows machines.

fyi In the same system I am also using the following line to populate an MVC dropdown:

<%: Html.DropDownListFor(model => model.Timezone,new SelectList(TimeZoneInfo.GetSystemTimeZones())) %>

Thanks

like image 781
Journeyman Avatar asked Nov 02 '25 02:11

Journeyman


1 Answers

You're using TimeZoneInfo.ToString(), which uses TimeZoneInfo.DisplayName. That simply is system-dependent - it's the same list (as far as I can tell) which is displayed if you go to edit the system time zone via the clock. (I've just looked on Vista and Windows 7 machines, and seen the same names you've just described.)

If you're looking for a consistent name, use the Id property instead. If you want a consistent mapping from ID to some known set of display names, you'll need to set that up yourself (e.g. via a single database all your systems talk to). You shouldn't display the Id property itself to users, as it's pretty confusing - it's almost always the standard time ID, which would confuse people when they're in daylight saving time.

If you want to push the boat out, the Unicode CLDR has information about how to display time zones to users (see core/common/bcp47/timezone.xml) but that uses the zoneinfo IDs, so you'd then need to map those back to the Windows system IDs (which is doable; that information is in CLDR as well, in core/common/supplemental/windowsZones.xml).

like image 162
Jon Skeet Avatar answered Nov 03 '25 16:11

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!