Get the default timezone for a country (via CultureInfo)

31,673

Solution 1

As identified in the comments of the question, you aren't going to be able to get a single time zone for each country. There are just too many cases of countries that have multiple time zones.

What you can do is filter the list of standard IANA/Olson time zones down to those available within a specific country.

One way to do this in C# is with Noda Time:

IEnumerable<string> zoneIds = TzdbDateTimeZoneSource.Default.ZoneLocations
    .Where(x => x.CountryCode == countryCode)
    .Select(x => x.ZoneId);

Pass a two-digit ISO-3166 country code, such as "AU" for Australia. The results are:

"Australia/Lord_Howe",
"Australia/Hobart",
"Australia/Currie",
"Australia/Melbourne",
"Australia/Sydney",
"Australia/Broken_Hill",
"Australia/Brisbane",
"Australia/Lindeman",
"Australia/Adelaide",
"Australia/Darwin",
"Australia/Perth",
"Australia/Eucla"

And if for some reason you'd like Windows time zone identifiers that you can use with the TimeZoneInfo object, Noda Time can map those too:

var source = TzdbDateTimeZoneSource.Default;
IEnumerable<string> windowsZoneIds = source.ZoneLocations
    .Where(x => x.CountryCode == countryCode)
    .Select(tz => source.WindowsMapping.MapZones
        .FirstOrDefault(x => x.TzdbIds.Contains(
                             source.CanonicalIdMap.First(y => y.Value == tz.ZoneId).Key)))
    .Where(x => x != null)
    .Select(x => x.WindowsId)
    .Distinct()

Again, called with "AU" for Australia returns:

"Tasmania Standard Time",
"AUS Eastern Standard Time",
"Cen. Australia Standard Time",
"E. Australia Standard Time",
"AUS Central Standard Time",
"W. Australia Standard Time"

If you're wondering about how reliable this data is, the country to tzid mapping is part of the IANA time zone database itself, in the zone.tab file. The IANA to Windows mapping data comes from the Unicode CLDR supplemental data. It doesn't get any closer to "official" than that.

Solution 2

May not be exactly what you are looking for, but try this: http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx

To get a specific time zone:

TimeZoneInfo tZone = TimeZoneInfo.FindSystemTimeZoneById("E. Australia Standard Time");

To see the available zones:

ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();

foreach (TimeZoneInfo zone in zones)
{
     Console.WriteLine(zone.Id);
}

Solution 3

In order to get CountryCode -> TimeZoneInfo mapping i used answer from Matt (2nd code snippet), but it didn't work for many cases. Found simpler and more reliable solution (using same Noda Time): TzdbDateTimeZoneSource.Default.WindowsMapping.MapZones basically has all the data.

Code sample:

Dictionary<string, TimeZoneInfo> GetIsoToTimeZoneMapping()
{
    var source = TzdbDateTimeZoneSource.Default;

    return source.WindowsMapping.MapZones
        .GroupBy(z => z.Territory)
        .ToDictionary(grp => grp.Key, grp => GetTimeZone(source, grp));
}

 TimeZoneInfo GetTimeZone(TzdbDateTimeZoneSource source, IEnumerable<MapZone> territoryLocations)
{
    var result = territoryLocations
        .Select(l => l.WindowsId)
        .Select(TimeZoneInfo.FindSystemTimeZoneById)
        //pick timezone with the minimum offset
        .Aggregate((tz1, tz2) => tz1.BaseUtcOffset < tz2.BaseUtcOffset ? tz1 : tz2);

    return result;
}
Share:
31,673
David Thielen
Author by

David Thielen

I have a wonderful wife, 3 terrific daughters, and 2 great dogs. In my spare time I read history and blog on political topics, including interviewing many of the elected officials here in Colorado (from both parties). I was CEO &amp; founder at Windward Studios (sold it in 2021) I've written operating systems (including on the Win95 team at Microsoft), games (including Enemy Nations), applications, firmware, and enterprise server systems. For the last 10 years I've been in both the Java and .NET world, mostly server side for Java, and both apps (Office AddIns) and server for C#. And for the last 3 years JavaScript/TypeScript also. (And truth to be told, since becoming CEO 2+ years ago I don't get to program much.) I've written a couple of books (including No Bugs! (free copy)) and numerous magazine articles. My email is [email protected] if you need to contact me.

Updated on July 09, 2022

Comments

  • David Thielen
    David Thielen almost 2 years

    Is there a program or a table that provides the default timezone for every country?

    Yes, the US, Canada, & Russia have multiple timezones. (I think every other country has just one.) But it's better to start on the most likely if a country is known rather than just provide a list starting at GMT.

    Preferably in C# but I'll take it in anything and convert to C#.