rioting bits

jedes herz ist eine revolutionäre zelle

Getting the current time in a different Time Zone (C#)

September 30th, 2009

A fast start with the solution (getting the current time in EST):

TimeZoneInfo timezone_EST = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

DateTime now_EST = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, timezone_EST);

And now for the yada yada.

As standard as the ‘Eastern Standard Time’ might seem to you, C# doesn’t know a thing about it. In fact, it doesn’t know much about any timezone, except the local timezone (the one on the computer running the application) and the UTC.

Why that, it beats me. I would have expected to have at least the more used timezones kept inside the basic library. But I guess it wasn’t meant to be.

Instead, to get information about a specific timezone, you have to retrieve it from your system’s registry, with the FindSystemTimeZoneById method. This means you have to hardcode the timezone’s ID, and this after you made sure the computer that runs the code has your intended timezone with that ID in the registry.

To get a complete list of the timezones available on your system, you can go ahead and check the registry! (just joking, there’s a method for that)

Fortunately, these timezone IDs are pretty standard across Windows installations. And I’ll just throw here a list, to easily grab an ID when I need it (these are the timezones on my Windows 7; compared with a list I got from a Windows XP, there are ~7 different entries, rather obscure timezones):

Id Display Name Supports DST
Afghanistan Standard Time (UTC+04:30) Kabul Nay.
Alaskan Standard Time (UTC-09:00) Alaska Yay!
Arab Standard Time (UTC+03:00) Kuwait, Riyadh Nay.
Arabian Standard Time (UTC+04:00) Abu Dhabi, Muscat Nay.
Arabic Standard Time (UTC+03:00) Baghdad Yay!
Argentina Standard Time (UTC-03:00) Buenos Aires Yay!
Atlantic Standard Time (UTC-04:00) Atlantic Time (Canada) Yay!
AUS Central Standard Time (UTC+09:30) Darwin Nay.
AUS Eastern Standard Time (UTC+10:00) Canberra, Melbourne, Sydney Yay!
Azerbaijan Standard Time (UTC+04:00) Baku Yay!
Azores Standard Time (UTC-01:00) Azores Yay!
Canada Central Standard Time (UTC-06:00) Saskatchewan Nay.
Cape Verde Standard Time (UTC-01:00) Cape Verde Is. Nay.
Caucasus Standard Time (UTC+04:00) Yerevan Yay!
Cen. Australia Standard Time (UTC+09:30) Adelaide Yay!
Central America Standard Time (UTC-06:00) Central America Nay.
Central Asia Standard Time (UTC+06:00) Astana, Dhaka Nay.
Central Brazilian Standard Time (UTC-04:00) Manaus Yay!
Central Europe Standard Time (UTC+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague Yay!
Central European Standard Time (UTC+01:00) Sarajevo, Skopje, Warsaw, Zagreb Yay!
Central Pacific Standard Time (UTC+11:00) Magadan, Solomon Is., New Caledonia Nay.
Central Standard Time (UTC-06:00) Central Time (US & Canada) Yay!
Central Standard Time (Mexico) (UTC-06:00) Guadalajara, Mexico City, Monterrey Yay!
China Standard Time (UTC+08:00) Beijing, Chongqing, Hong Kong, Urumqi Nay.
Dateline Standard Time (UTC-12:00) International Date Line West Nay.
E. Africa Standard Time (UTC+03:00) Nairobi Nay.
E. Australia Standard Time (UTC+10:00) Brisbane Nay.
E. Europe Standard Time (UTC+02:00) Minsk Yay!
E. South America Standard Time (UTC-03:00) Brasilia Yay!
Eastern Standard Time (UTC-05:00) Eastern Time (US & Canada) Yay!
Egypt Standard Time (UTC+02:00) Cairo Yay!
Ekaterinburg Standard Time (UTC+05:00) Ekaterinburg Yay!
Fiji Standard Time (UTC+12:00) Fiji, Kamchatka, Marshall Is. Nay.
FLE Standard Time (UTC+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius Yay!
Georgian Standard Time (UTC+03:00) Tbilisi Nay.
GMT Standard Time (UTC) Dublin, Edinburgh, Lisbon, London Yay!
Greenland Standard Time (UTC-03:00) Greenland Yay!
Greenwich Standard Time (UTC) Monrovia, Reykjavik Nay.
GTB Standard Time (UTC+02:00) Athens, Bucharest, Istanbul Yay!
Hawaiian Standard Time (UTC-10:00) Hawaii Nay.
India Standard Time (UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi Nay.
Iran Standard Time (UTC+03:30) Tehran Yay!
Israel Standard Time (UTC+02:00) Jerusalem Yay!
Jordan Standard Time (UTC+02:00) Amman Yay!
Korea Standard Time (UTC+09:00) Seoul Nay.
Mauritius Standard Time (UTC+04:00) Port Louis Yay!
Mid-Atlantic Standard Time (UTC-02:00) Mid-Atlantic Yay!
Middle East Standard Time (UTC+02:00) Beirut Yay!
Montevideo Standard Time (UTC-03:00) Montevideo Yay!
Morocco Standard Time (UTC) Casablanca Yay!
Mountain Standard Time (UTC-07:00) Mountain Time (US & Canada) Yay!
Mountain Standard Time (Mexico) (UTC-07:00) Chihuahua, La Paz, Mazatlan Yay!
Myanmar Standard Time (UTC+06:30) Yangon (Rangoon) Nay.
N. Central Asia Standard Time (UTC+06:00) Almaty, Novosibirsk Yay!
Namibia Standard Time (UTC+02:00) Windhoek Yay!
Nepal Standard Time (UTC+05:45) Kathmandu Nay.
New Zealand Standard Time (UTC+12:00) Auckland, Wellington Yay!
Newfoundland Standard Time (UTC-03:30) Newfoundland Yay!
North Asia East Standard Time (UTC+08:00) Irkutsk, Ulaan Bataar Yay!
North Asia Standard Time (UTC+07:00) Krasnoyarsk Yay!
Pacific SA Standard Time (UTC-04:00) Santiago Yay!
Pacific Standard Time (UTC-08:00) Pacific Time (US & Canada) Yay!
Pacific Standard Time (Mexico) (UTC-08:00) Tijuana, Baja California Yay!
Pakistan Standard Time (UTC+05:00) Islamabad, Karachi Yay!
Romance Standard Time (UTC+01:00) Brussels, Copenhagen, Madrid, Paris Yay!
Russian Standard Time (UTC+03:00) Moscow, St. Petersburg, Volgograd Yay!
SA Eastern Standard Time (UTC-03:00) Georgetown Nay.
SA Pacific Standard Time (UTC-05:00) Bogota, Lima, Quito, Rio Branco Nay.
SA Western Standard Time (UTC-04:00) La Paz Nay.
Samoa Standard Time (UTC-11:00) Midway Island, Samoa Nay.
SE Asia Standard Time (UTC+07:00) Bangkok, Hanoi, Jakarta Nay.
Singapore Standard Time (UTC+08:00) Kuala Lumpur, Singapore Nay.
South Africa Standard Time (UTC+02:00) Harare, Pretoria Nay.
Sri Lanka Standard Time (UTC+05:30) Sri Jayawardenepura Nay.
Taipei Standard Time (UTC+08:00) Taipei Nay.
Tasmania Standard Time (UTC+10:00) Hobart Yay!
Tokyo Standard Time (UTC+09:00) Osaka, Sapporo, Tokyo Nay.
Tonga Standard Time (UTC+13:00) Nuku’alofa Nay.
US Eastern Standard Time (UTC-05:00) Indiana (East) Nay.
US Mountain Standard Time (UTC-07:00) Arizona Nay.
UTC (UTC) Coordinated Universal Time Nay.
Venezuela Standard Time (UTC-04:30) Caracas Nay.
Vladivostok Standard Time (UTC+10:00) Vladivostok Yay!
W. Australia Standard Time (UTC+08:00) Perth Yay!
W. Central Africa Standard Time (UTC+01:00) West Central Africa Nay.
W. Europe Standard Time (UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna Yay!
West Asia Standard Time (UTC+05:00) Tashkent Nay.
West Pacific Standard Time (UTC+10:00) Guam, Port Moresby Nay.
Yakutsk Standard Time (UTC+09:00) Yakutsk Yay!

 

If you like reading in-depth articles from the Creators, I highly recommend a post called A Brief History of DateTime, not by Stephen Hawking, on the BCL Team Blog. It’s about how the DateTime types evolved across .NET versions and how, in order to preserve backwards-compatibility and reliability, the usage of these types may not be that intuitive in some cases.

Leave a Reply