Journeyman
Journeyman

Reputation: 10271

TimeZoneInfo.GetSystemTimeZones() returns inconsistent information on different computers

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

Upvotes: 4

Views: 2110

Answers (2)

SAinCA
SAinCA

Reputation: 194

RE: "Not 100% sure about this, but it seems Vista lists (GMT) values, and Windows 7 lists (UTC) values, and Windows Server lists (GMT)"

Windows Server 2008 R2 returns UTC. Server 2003 returned GMT.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499840

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).

Upvotes: 5

Related Questions