Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.TimeZoneNotFoundException error while getting datetime for particular time zone in c#

Tags:

timezone

c#

I've a JSON file which has time_zone parameter. It has values like London, Casablanca, Arizona, Pacific Time (US & Canada) etc. Based on the time_zone, I want to get the DateTime result of that time zone.

For example,

Current time = 8/3/2015 4:00:00 PM
 
If time_zone = Central Time (US & Canada) then result should be 8/3/2015 3:00:00 PM,

If time_zone = Pacific Time (US & Canada) then result should be 8/3/2015 1:00:00 PM,

If time_zone = London then result should be 8/3/2015 9:00:00 PM and so on.

I want to display the current time as well as time as per given time zone.

I tried following approach to get time for respected time zone but it is throwing an error.

Code: Works

 DateTime timeUtc = DateTime.Now;
 TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
 var b = TimeZoneInfo.ConvertTime(timeUtc, cstZone);

Error : The time zone ID 'Central Time (US & Canada)' was not found on the local computer.

 TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Time (US & Canada)");
 var b = TimeZoneInfo.ConvertTime(timeUtc , cstZone);

Also tried but same error,

var c = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(timeUtc, "Central Time (US & Canada)");

My issue is, time_zone values are in Central Time (US & Canada), Pacific Time (US & Canada), etc. format and I've to work with that as well as different time zones like London, Casablanca . How to handle this kind of scenarios.

like image 291
GThree Avatar asked Aug 03 '15 20:08

GThree


People also ask

How do I get timezone with datetime?

You can get the current time in a particular timezone by using the datetime module with another module called pytz . You can then check for all available timezones with the snippet below: from datetime import datetime import pytz zones = pytz. all_timezones print(zones) # Output: all timezones of the world.

What is TimeZoneid?

The Time zone ID column lists time zones, in boldface, and the locations within each time zone. The Raw offset column lists the difference, in hours and minutes, between Greenwich Mean Time (GMT) and the specified time zone. The DST offset column lists the offset, in minutes, for Daylight Savings Time (DST).


1 Answers

Assuming the time zone identifiers in your file came from Rails, then you are basically asking the reverse of this question.

Similar to the answer of that question, we will use the Rails mapping, along with CLDR mappings, to obtain a Rails-To-Windows mapping constant in C#.

public static class TZConvert
{
    public static string RailsToWindows(string railsTimeZoneId)
    {
        return RailsWindowsMapping.ContainsKey(railsTimeZoneId)
            ? RailsWindowsMapping[railsTimeZoneId]
            : null;
    }

    private static readonly IReadOnlyDictionary<string, string> RailsWindowsMapping =
        new Dictionary<string, string>
        {
            {"Abu Dhabi", "Arabian Standard Time"},
            {"Adelaide", "Cen. Australia Standard Time"},
            {"Alaska", "Alaskan Standard Time"},
            {"Almaty", "Central Asia Standard Time"},
            {"American Samoa", "UTC-11"},
            {"Amsterdam", "W. Europe Standard Time"},
            {"Arizona", "US Mountain Standard Time"},
            {"Astana", "Bangladesh Standard Time"},
            {"Athens", "GTB Standard Time"},
            {"Atlantic Time (Canada)", "Atlantic Standard Time"},
            {"Auckland", "New Zealand Standard Time"},
            {"Azores", "Azores Standard Time"},
            {"Baghdad", "Arabic Standard Time"},
            {"Baku", "Azerbaijan Standard Time"},
            {"Bangkok", "SE Asia Standard Time"},
            {"Beijing", "China Standard Time"},
            {"Belgrade", "Central Europe Standard Time"},
            {"Berlin", "W. Europe Standard Time"},
            {"Bern", "W. Europe Standard Time"},
            {"Bogota", "SA Pacific Standard Time"},
            {"Brasilia", "E. South America Standard Time"},
            {"Bratislava", "Central Europe Standard Time"},
            {"Brisbane", "E. Australia Standard Time"},
            {"Brussels", "Romance Standard Time"},
            {"Bucharest", "GTB Standard Time"},
            {"Budapest", "Central Europe Standard Time"},
            {"Buenos Aires", "Argentina Standard Time"},
            {"Cairo", "Egypt Standard Time"},
            {"Canberra", "AUS Eastern Standard Time"},
            {"Cape Verde Is.", "Cape Verde Standard Time"},
            {"Caracas", "Venezuela Standard Time"},
            {"Casablanca", "Morocco Standard Time"},
            {"Central America", "Central America Standard Time"},
            {"Central Time (US & Canada)", "Central Standard Time"},
            {"Chennai", "India Standard Time"},
            {"Chihuahua", "Mountain Standard Time (Mexico)"},
            {"Chongqing", "China Standard Time"},
            {"Copenhagen", "Romance Standard Time"},
            {"Darwin", "AUS Central Standard Time"},
            {"Dhaka", "Bangladesh Standard Time"},
            {"Dublin", "GMT Standard Time"},
            {"Eastern Time (US & Canada)", "Eastern Standard Time"},
            {"Edinburgh", "GMT Standard Time"},
            {"Ekaterinburg", "Ekaterinburg Standard Time"},
            {"Fiji", "Fiji Standard Time"},
            {"Georgetown", "SA Western Standard Time"},
            {"Greenland", "Greenland Standard Time"},
            {"Guadalajara", "Central Standard Time (Mexico)"},
            {"Guam", "West Pacific Standard Time"},
            {"Hanoi", "SE Asia Standard Time"},
            {"Harare", "South Africa Standard Time"},
            {"Hawaii", "Hawaiian Standard Time"},
            {"Helsinki", "FLE Standard Time"},
            {"Hobart", "Tasmania Standard Time"},
            {"Hong Kong", "China Standard Time"},
            {"Indiana (East)", "US Eastern Standard Time"},
            {"International Date Line West", "UTC-11"},
            {"Irkutsk", "North Asia East Standard Time"},
            {"Islamabad", "Pakistan Standard Time"},
            {"Istanbul", "Turkey Standard Time"},
            {"Jakarta", "SE Asia Standard Time"},
            {"Jerusalem", "Israel Standard Time"},
            {"Kabul", "Afghanistan Standard Time"},
            {"Kaliningrad", "Kaliningrad Standard Time"},
            {"Kamchatka", "Russia Time Zone 11"},
            {"Karachi", "Pakistan Standard Time"},
            {"Kathmandu", "Nepal Standard Time"},
            {"Kolkata", "India Standard Time"},
            {"Krasnoyarsk", "North Asia Standard Time"},
            {"Kuala Lumpur", "Singapore Standard Time"},
            {"Kuwait", "Arab Standard Time"},
            {"Kyiv", "FLE Standard Time"},
            {"La Paz", "SA Western Standard Time"},
            {"Lima", "SA Pacific Standard Time"},
            {"Lisbon", "GMT Standard Time"},
            {"Ljubljana", "Central Europe Standard Time"},
            {"London", "GMT Standard Time"},
            {"Madrid", "Romance Standard Time"},
            {"Magadan", "Magadan Standard Time"},
            {"Marshall Is.", "UTC+12"},
            {"Mazatlan", "Mountain Standard Time (Mexico)"},
            {"Melbourne", "AUS Eastern Standard Time"},
            {"Mexico City", "Central Standard Time (Mexico)"},
            {"Mid-Atlantic", "UTC-02"},
            {"Midway Island", "UTC-11"},
            {"Minsk", "Belarus Standard Time"},
            {"Monrovia", "Greenwich Standard Time"},
            {"Monterrey", "Central Standard Time (Mexico)"},
            {"Montevideo", "Montevideo Standard Time"},
            {"Moscow", "Russian Standard Time"},
            {"Mountain Time (US & Canada)", "Mountain Standard Time"},
            {"Mumbai", "India Standard Time"},
            {"Muscat", "Arabian Standard Time"},
            {"Nairobi", "E. Africa Standard Time"},
            {"New Caledonia", "Central Pacific Standard Time"},
            {"New Delhi", "India Standard Time"},
            {"Newfoundland", "Newfoundland Standard Time"},
            {"Novosibirsk", "N. Central Asia Standard Time"},
            {"Nuku'alofa", "Tonga Standard Time"},
            {"Osaka", "Tokyo Standard Time"},
            {"Pacific Time (US & Canada)", "Pacific Standard Time"},
            {"Paris", "Romance Standard Time"},
            {"Perth", "W. Australia Standard Time"},
            {"Port Moresby", "West Pacific Standard Time"},
            {"Prague", "Central Europe Standard Time"},
            {"Pretoria", "South Africa Standard Time"},
            {"Quito", "SA Pacific Standard Time"},
            {"Rangoon", "Myanmar Standard Time"},
            {"Riga", "FLE Standard Time"},
            {"Riyadh", "Arab Standard Time"},
            {"Rome", "W. Europe Standard Time"},
            {"Samara", "Russia Time Zone 3"},
            {"Samoa", "Samoa Standard Time"},
            {"Santiago", "Pacific SA Standard Time"},
            {"Sapporo", "Tokyo Standard Time"},
            {"Sarajevo", "Central European Standard Time"},
            {"Saskatchewan", "Canada Central Standard Time"},
            {"Seoul", "Korea Standard Time"},
            {"Singapore", "Singapore Standard Time"},
            {"Skopje", "Central European Standard Time"},
            {"Sofia", "FLE Standard Time"},
            {"Solomon Is.", "Central Pacific Standard Time"},
            {"Srednekolymsk", "Russia Time Zone 10"},
            {"Sri Jayawardenepura", "Sri Lanka Standard Time"},
            {"St. Petersburg", "Russian Standard Time"},
            {"Stockholm", "W. Europe Standard Time"},
            {"Sydney", "AUS Eastern Standard Time"},
            {"Taipei", "Taipei Standard Time"},
            {"Tallinn", "FLE Standard Time"},
            {"Tashkent", "West Asia Standard Time"},
            {"Tbilisi", "Georgian Standard Time"},
            {"Tehran", "Iran Standard Time"},
            {"Tijuana", "Pacific Standard Time"},
            {"Tokelau Is.", "Tonga Standard Time"},
            {"Tokyo", "Tokyo Standard Time"},
            {"Ulaanbaatar", "Ulaanbaatar Standard Time"},
            {"Urumqi", "Central Asia Standard Time"},
            {"UTC", "UTC"},
            {"Vienna", "W. Europe Standard Time"},
            {"Vilnius", "FLE Standard Time"},
            {"Vladivostok", "Vladivostok Standard Time"},
            {"Volgograd", "Russian Standard Time"},
            {"Warsaw", "Central European Standard Time"},
            {"Wellington", "New Zealand Standard Time"},
            {"West Central Africa", "W. Central Africa Standard Time"},
            {"Yakutsk", "Yakutsk Standard Time"},
            {"Yerevan", "Caucasus Standard Time"},
            {"Zagreb", "Central European Standard Time"}
        };
}

You can now use that with TimeZoneInfo like so:

string windowsZoneId = TZConvert.RailsToWindows("Central Time (US & Canada)");
TimeZoneInfo central = TimeZoneInfo.FindSystemTimeZoneById(windowsZoneId);

Be aware of a few things:

  • Rails mappings seem to be relatively fixed, but CLDR mappings are occasionally updated. You may need to revisit this from time to time to make sure any new time zones are mapped appropriately.

  • The Rails time zone "Chatham Is." cannot currently be mapped to any Windows time zone. (There is no time zone defined for Chatham Islands in Windows.)

  • If you wanted to be more accurate and support all zones, you could go from Rails to IANA/TZDB time zones instead of to Windows time zones. You could then use Noda Time with it's native TZDB support. This would also remove the need to use CLDR mappings.

like image 165
Matt Johnson-Pint Avatar answered Sep 19 '22 12:09

Matt Johnson-Pint