What Value Should I Pass into Timezoneinfo.Findsystemtimezonebyid(String)

List of Timezone IDs for use with FindTimeZoneById() in C#?

Here's a full listing of a program and its results.

The code:

using System;

namespace TimeZoneIds
{
class Program
{
static void Main(string[] args)
{
foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones())
{
// For a Console App
Console.WriteLine(z.Id + "," + z.BaseUtcOffset + "," + z.StandardName + "," + z.DisplayName + "," + z.DaylightName);
// For any other App
System.Diagnostics.Debug.WriteLine(z.Id + "," + z.BaseUtcOffset + "," + z.StandardName + "," + z.DisplayName + "," + z.DaylightName);
}
}
}
}

Results can change over time:

Sample Image

Strange error with converting from UTC to TimeZone

Refer to the documentation of the TimeZoneInfo.ConvertTime(DateTime, TimeZoneInfo) method.

In the Remarks section, it says that if the Kind property of the source DateTime is DateTimeKind.Unspecified, that it is assumed to be local. Since you explicitly set the kind to Unspecified in your code, you are not converting from UTC to the named time zone, but you are instead converting from local time to the named time zone.

In this context, "local time" means local to the time zone setting of the computer where the code is executing. So if your server in Poland is set for Poland's time zone, then the conversion to Poland's time will be a no-op.

If your intention is to convert UTC to a specific time zone, then either the source value's Kind should be DateTimeKind.Utc, or you should use ConvertTimeFromUtc instead of ConvertTime (the difference being, Unspecified kind is assumed to be Utc rather than Local).

As an aside - you've got a potential bug with DateTime.Parse(property.GetValue(value).ToString(). Don't ever create a string from an object just to parse it back to an object again. Instead, cast the object to unbox it to the desired type. The tostring/parse approach often is slower and often introduces bugs related to date formats of the current culture.

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

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.



Related Topics



Leave a reply



Submit