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#.
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:
To see the available zones:
Latest Windows versions contain file
%WINDIR%\Globalization\Time Zone\timezoneMapping.xml
which maps Olson to Windows time zone, you can query it as regularXML
. I don't know but maybeC#
already has a class which works with it.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:
Pass a two-digit ISO-3166 country code, such as
"AU"
for Australia. The results are: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:Again, called with
"AU"
for Australia returns: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.
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: