TimeZoneInfo 在 Mac/Linux 找不到指定 time zone ID

同事提到 TimeZoneInfo 的操作在 Linux server 會出現錯誤,想說我怎麼沒遇到立馬試試,於是發現原來是 time zone ID 在 Windows 與 Linux 不同造成的 XD 雖說可以理解,但不免還是有些違詞:微軟這類的問題在過去可是層出不窮,就是要跟別人不一樣 XD 幸虧這個狀況近幾年有大幅改善 (我個人最有感的就是 docker command 的 porting,不同平台有幾乎一致的使用體驗),問題既然已經出現就得要找解決方式,紀錄一下用法備查

基本環境說明

  1. Mac/Linux : macOS Mojave 10.14.4
  2. Windows : https://dotnetfiddle.net/ 與 Windows 10 Version 1803 (OS Build 17134.590)
  3. .NET Core 2.2.101
  4. NuGet 套件
    • TimeZoneConverter 3.1.0
  5. 模擬程式碼

    TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time");
    var utcNow = DateTime.UtcNow;
    Console.WriteLine(utcNow);
    Console.WriteLine(TimeZoneInfo.ConvertTime(utcNow, timeZone));
    
  6. Windows 平台結果

    5/10/2019 7:05:46 AM
    5/10/2019 3:05:46 PM
    

錯誤訊息

  1. 訊息內容

    Unhandled Exception: System.TimeZoneNotFoundException: The time zone ID 'China Standard Time' was not found on the local computer. ---> System.IO.FileNotFoundException: Could not find file '/usr/share/zoneinfo/China Standard Time'.
    at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)
    at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode)
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
    at Internal.IO.File.ReadAllBytes(String path)
    at System.TimeZoneInfo.TryGetTimeZoneFromLocalMachine(String id, TimeZoneInfo& value, Exception& e)
    --- End of inner exception stack trace ---
    at System.TimeZoneInfo.FindSystemTimeZoneById(String id)
    at TestFluentd.Program.Main(String[] args) in /Users/yowko.tsai/Program.cs:line 30
    
    Process finished with exit code 6.
    
  2. 錯誤截圖

    1error

解決方式 1 : 使用套件 TimeZoneConverter

  1. 安裝套件

    • Package Manager

      Install-Package TimeZoneConverter
      
    • .NET CLI

      dotnet add package TimeZoneConverter
      
  2. 實際使用方式

    TimeZoneInfo timeZone =TZConvert.GetTimeZoneInfo("China Standard Time");
    var utcNow = DateTime.UtcNow;
    Console.WriteLine(utcNow);
    Console.WriteLine(TimeZoneInfo.ConvertTime(utcNow, timeZone));
    

解決方式 2 : 使用 TZ database name

  1. Windows time zone 與 TZ ID 對應

    WindowsTZ ID
    AUS Central Standard TimeAustralia/Darwin
    AUS Central Standard TimeAustralia/Darwin
    AUS Eastern Standard TimeAustralia/Sydney
    AUS Eastern Standard TimeAustralia/Sydney Australia/Melbourne
    Afghanistan Standard TimeAsia/Kabul
    Afghanistan Standard TimeAsia/Kabul
    Alaskan Standard TimeAmerica/Anchorage
    Alaskan Standard TimeAmerica/Anchorage America/Juneau America/Nome America/Sitka America/Yakutat
    Aleutian Standard TimeAmerica/Adak
    Aleutian Standard TimeAmerica/Adak
    Altai Standard TimeAsia/Barnaul
    Altai Standard TimeAsia/Barnaul
    Arab Standard TimeAsia/Riyadh
    Arab Standard TimeAsia/Bahrain
    Arab Standard TimeAsia/Kuwait
    Arab Standard TimeAsia/Qatar
    Arab Standard TimeAsia/Riyadh
    Arab Standard TimeAsia/Aden
    Arabian Standard TimeAsia/Dubai
    Arabian Standard TimeAsia/Dubai
    Arabian Standard TimeAsia/Muscat
    Arabian Standard TimeEtc/GMT-4
    Arabic Standard TimeAsia/Baghdad
    Arabic Standard TimeAsia/Baghdad
    Argentina Standard TimeAmerica/Buenos_Aires
    Argentina Standard TimeAmerica/Buenos_Aires America/Argentina/La_Rioja America/Argentina/Rio_Gallegos America/Argentina/Salta America/Argentina/San_Juan America/Argentina/San_Luis America/Argentina/Tucuman America/Argentina/Ushuaia America/Catamarca America/Cordoba America/Jujuy America/Mendoza
    Astrakhan Standard TimeEurope/Astrakhan
    Astrakhan Standard TimeEurope/Astrakhan Europe/Ulyanovsk
    Atlantic Standard TimeAmerica/Halifax
    Atlantic Standard TimeAtlantic/Bermuda
    Atlantic Standard TimeAmerica/Halifax America/Glace_Bay America/Goose_Bay America/Moncton
    Atlantic Standard TimeAmerica/Thule
    Aus Central W. Standard TimeAustralia/Eucla
    Aus Central W. Standard TimeAustralia/Eucla
    Azerbaijan Standard TimeAsia/Baku
    Azerbaijan Standard TimeAsia/Baku
    Azores Standard TimeAtlantic/Azores
    Azores Standard TimeAmerica/Scoresbysund
    Azores Standard TimeAtlantic/Azores
    Bahia Standard TimeAmerica/Bahia
    Bahia Standard TimeAmerica/Bahia
    Bangladesh Standard TimeAsia/Dhaka
    Bangladesh Standard TimeAsia/Dhaka
    Bangladesh Standard TimeAsia/Thimphu
    Belarus Standard TimeEurope/Minsk
    Belarus Standard TimeEurope/Minsk
    Bougainville Standard TimePacific/Bougainville
    Bougainville Standard TimePacific/Bougainville
    Canada Central Standard TimeAmerica/Regina
    Canada Central Standard TimeAmerica/Regina America/Swift_Current
    Cape Verde Standard TimeAtlantic/Cape_Verde
    Cape Verde Standard TimeAtlantic/Cape_Verde
    Cape Verde Standard TimeEtc/GMT+1
    Caucasus Standard TimeAsia/Yerevan
    Caucasus Standard TimeAsia/Yerevan
    Cen. Australia Standard TimeAustralia/Adelaide
    Cen. Australia Standard TimeAustralia/Adelaide Australia/Broken_Hill
    Central America Standard TimeAmerica/Guatemala
    Central America Standard TimeAmerica/Belize
    Central America Standard TimeAmerica/Costa_Rica
    Central America Standard TimePacific/Galapagos
    Central America Standard TimeAmerica/Guatemala
    Central America Standard TimeAmerica/Tegucigalpa
    Central America Standard TimeAmerica/Managua
    Central America Standard TimeAmerica/El_Salvador
    Central America Standard TimeEtc/GMT+6
    Central Asia Standard TimeAsia/Almaty
    Central Asia Standard TimeAntarctica/Vostok
    Central Asia Standard TimeAsia/Urumqi
    Central Asia Standard TimeIndian/Chagos
    Central Asia Standard TimeAsia/Bishkek
    Central Asia Standard TimeAsia/Almaty Asia/Qostanay
    Central Asia Standard TimeEtc/GMT-6
    Central Brazilian Standard TimeAmerica/Cuiaba
    Central Brazilian Standard TimeAmerica/Cuiaba America/Campo_Grande
    Central Europe Standard TimeEurope/Budapest
    Central Europe Standard TimeEurope/Tirane
    Central Europe Standard TimeEurope/Prague
    Central Europe Standard TimeEurope/Budapest
    Central Europe Standard TimeEurope/Podgorica
    Central Europe Standard TimeEurope/Belgrade
    Central Europe Standard TimeEurope/Ljubljana
    Central Europe Standard TimeEurope/Bratislava
    Central European Standard TimeEurope/Warsaw
    Central European Standard TimeEurope/Sarajevo
    Central European Standard TimeEurope/Zagreb
    Central European Standard TimeEurope/Skopje
    Central European Standard TimeEurope/Warsaw
    Central Pacific Standard TimePacific/Guadalcanal
    Central Pacific Standard TimeAntarctica/Macquarie
    Central Pacific Standard TimePacific/Ponape Pacific/Kosrae
    Central Pacific Standard TimePacific/Noumea
    Central Pacific Standard TimePacific/Guadalcanal
    Central Pacific Standard TimePacific/Efate
    Central Pacific Standard TimeEtc/GMT-11
    Central Standard TimeAmerica/Chicago
    Central Standard TimeAmerica/Winnipeg America/Rainy_River America/Rankin_Inlet America/Resolute
    Central Standard TimeAmerica/Matamoros
    Central Standard TimeAmerica/Chicago America/Indiana/Knox America/Indiana/Tell_City America/Menominee America/North_Dakota/Beulah America/North_Dakota/Center America/North_Dakota/New_Salem
    Central Standard TimeCST6CDT
    Central Standard Time (Mexico)America/Mexico_City
    Central Standard Time (Mexico)America/Mexico_City America/Bahia_Banderas America/Merida America/Monterrey
    Chatham Islands Standard TimePacific/Chatham
    Chatham Islands Standard TimePacific/Chatham
    China Standard TimeAsia/Shanghai
    China Standard TimeAsia/Shanghai
    China Standard TimeAsia/Hong_Kong
    China Standard TimeAsia/Macau
    Cuba Standard TimeAmerica/Havana
    Cuba Standard TimeAmerica/Havana
    Dateline Standard TimeEtc/GMT+12
    Dateline Standard TimeEtc/GMT+12
    E. Africa Standard TimeAfrica/Nairobi
    E. Africa Standard TimeAntarctica/Syowa
    E. Africa Standard TimeAfrica/Djibouti
    E. Africa Standard TimeAfrica/Asmera
    E. Africa Standard TimeAfrica/Addis_Ababa
    E. Africa Standard TimeAfrica/Nairobi
    E. Africa Standard TimeIndian/Comoro
    E. Africa Standard TimeIndian/Antananarivo
    E. Africa Standard TimeAfrica/Mogadishu
    E. Africa Standard TimeAfrica/Juba
    E. Africa Standard TimeAfrica/Dar_es_Salaam
    E. Africa Standard TimeAfrica/Kampala
    E. Africa Standard TimeIndian/Mayotte
    E. Africa Standard TimeEtc/GMT-3
    E. Australia Standard TimeAustralia/Brisbane
    E. Australia Standard TimeAustralia/Brisbane Australia/Lindeman
    E. Europe Standard TimeEurope/Chisinau
    E. Europe Standard TimeEurope/Chisinau
    E. South America Standard TimeAmerica/Sao_Paulo
    E. South America Standard TimeAmerica/Sao_Paulo
    Easter Island Standard TimePacific/Easter
    Easter Island Standard TimePacific/Easter
    Eastern Standard TimeAmerica/New_York
    Eastern Standard TimeAmerica/Nassau
    Eastern Standard TimeAmerica/Toronto America/Iqaluit America/Montreal America/Nipigon America/Pangnirtung America/Thunder_Bay
    Eastern Standard TimeAmerica/New_York America/Detroit America/Indiana/Petersburg America/Indiana/Vincennes America/Indiana/Winamac America/Kentucky/Monticello America/Louisville
    Eastern Standard TimeEST5EDT
    Eastern Standard Time (Mexico)America/Cancun
    Eastern Standard Time (Mexico)America/Cancun
    Egypt Standard TimeAfrica/Cairo
    Egypt Standard TimeAfrica/Cairo
    Ekaterinburg Standard TimeAsia/Yekaterinburg
    Ekaterinburg Standard TimeAsia/Yekaterinburg
    FLE Standard TimeEurope/Kiev
    FLE Standard TimeEurope/Mariehamn
    FLE Standard TimeEurope/Sofia
    FLE Standard TimeEurope/Tallinn
    FLE Standard TimeEurope/Helsinki
    FLE Standard TimeEurope/Vilnius
    FLE Standard TimeEurope/Riga
    FLE Standard TimeEurope/Kiev Europe/Uzhgorod Europe/Zaporozhye
    Fiji Standard TimePacific/Fiji
    Fiji Standard TimePacific/Fiji
    GMT Standard TimeEurope/London
    GMT Standard TimeAtlantic/Canary
    GMT Standard TimeAtlantic/Faeroe
    GMT Standard TimeEurope/London
    GMT Standard TimeEurope/Guernsey
    GMT Standard TimeEurope/Dublin
    GMT Standard TimeEurope/Isle_of_Man
    GMT Standard TimeEurope/Jersey
    GMT Standard TimeEurope/Lisbon Atlantic/Madeira
    GTB Standard TimeEurope/Bucharest
    GTB Standard TimeAsia/Famagusta Asia/Nicosia
    GTB Standard TimeEurope/Athens
    GTB Standard TimeEurope/Bucharest
    Georgian Standard TimeAsia/Tbilisi
    Georgian Standard TimeAsia/Tbilisi
    Greenland Standard TimeAmerica/Godthab
    Greenland Standard TimeAmerica/Godthab
    Greenwich Standard TimeAtlantic/Reykjavik
    Greenwich Standard TimeAfrica/Ouagadougou
    Greenwich Standard TimeAfrica/Abidjan
    Greenwich Standard TimeAfrica/Accra
    Greenwich Standard TimeAfrica/Banjul
    Greenwich Standard TimeAfrica/Conakry
    Greenwich Standard TimeAfrica/Bissau
    Greenwich Standard TimeAtlantic/Reykjavik
    Greenwich Standard TimeAfrica/Monrovia
    Greenwich Standard TimeAfrica/Bamako
    Greenwich Standard TimeAfrica/Nouakchott
    Greenwich Standard TimeAtlantic/St_Helena
    Greenwich Standard TimeAfrica/Freetown
    Greenwich Standard TimeAfrica/Dakar
    Greenwich Standard TimeAfrica/Lome
    Haiti Standard TimeAmerica/Port-au-Prince
    Haiti Standard TimeAmerica/Port-au-Prince
    Hawaiian Standard TimePacific/Honolulu
    Hawaiian Standard TimePacific/Rarotonga
    Hawaiian Standard TimePacific/Tahiti
    Hawaiian Standard TimePacific/Johnston
    Hawaiian Standard TimePacific/Honolulu
    Hawaiian Standard TimeEtc/GMT+10
    India Standard TimeAsia/Calcutta
    India Standard TimeAsia/Calcutta
    Iran Standard TimeAsia/Tehran
    Iran Standard TimeAsia/Tehran
    Israel Standard TimeAsia/Jerusalem
    Israel Standard TimeAsia/Jerusalem
    Jordan Standard TimeAsia/Amman
    Jordan Standard TimeAsia/Amman
    Kaliningrad Standard TimeEurope/Kaliningrad
    Kaliningrad Standard TimeEurope/Kaliningrad
    Korea Standard TimeAsia/Seoul
    Korea Standard TimeAsia/Seoul
    Libya Standard TimeAfrica/Tripoli
    Libya Standard TimeAfrica/Tripoli
    Line Islands Standard TimePacific/Kiritimati
    Line Islands Standard TimePacific/Kiritimati
    Line Islands Standard TimeEtc/GMT-14
    Lord Howe Standard TimeAustralia/Lord_Howe
    Lord Howe Standard TimeAustralia/Lord_Howe
    Magadan Standard TimeAsia/Magadan
    Magadan Standard TimeAsia/Magadan
    Magallanes Standard TimeAmerica/Punta_Arenas
    Magallanes Standard TimeAntarctica/Palmer
    Magallanes Standard TimeAmerica/Punta_Arenas
    Marquesas Standard TimePacific/Marquesas
    Marquesas Standard TimePacific/Marquesas
    Mauritius Standard TimeIndian/Mauritius
    Mauritius Standard TimeIndian/Mauritius
    Mauritius Standard TimeIndian/Reunion
    Mauritius Standard TimeIndian/Mahe
    Middle East Standard TimeAsia/Beirut
    Middle East Standard TimeAsia/Beirut
    Montevideo Standard TimeAmerica/Montevideo
    Montevideo Standard TimeAmerica/Montevideo
    Morocco Standard TimeAfrica/Casablanca
    Morocco Standard TimeAfrica/El_Aaiun
    Morocco Standard TimeAfrica/Casablanca
    Mountain Standard TimeAmerica/Denver
    Mountain Standard TimeAmerica/Edmonton America/Cambridge_Bay America/Inuvik America/Yellowknife
    Mountain Standard TimeAmerica/Ojinaga
    Mountain Standard TimeAmerica/Denver America/Boise
    Mountain Standard TimeMST7MDT
    Mountain Standard Time (Mexico)America/Chihuahua
    Mountain Standard Time (Mexico)America/Chihuahua America/Mazatlan
    Myanmar Standard TimeAsia/Rangoon
    Myanmar Standard TimeIndian/Cocos
    Myanmar Standard TimeAsia/Rangoon
    N. Central Asia Standard TimeAsia/Novosibirsk
    N. Central Asia Standard TimeAsia/Novosibirsk
    Namibia Standard TimeAfrica/Windhoek
    Namibia Standard TimeAfrica/Windhoek
    Nepal Standard TimeAsia/Katmandu
    Nepal Standard TimeAsia/Katmandu
    New Zealand Standard TimePacific/Auckland
    New Zealand Standard TimeAntarctica/McMurdo
    New Zealand Standard TimePacific/Auckland
    Newfoundland Standard TimeAmerica/St_Johns
    Newfoundland Standard TimeAmerica/St_Johns
    Norfolk Standard TimePacific/Norfolk
    Norfolk Standard TimePacific/Norfolk
    North Asia East Standard TimeAsia/Irkutsk
    North Asia East Standard TimeAsia/Irkutsk
    North Asia Standard TimeAsia/Krasnoyarsk
    North Asia Standard TimeAsia/Krasnoyarsk Asia/Novokuznetsk
    North Korea Standard TimeAsia/Pyongyang
    North Korea Standard TimeAsia/Pyongyang
    Omsk Standard TimeAsia/Omsk
    Omsk Standard TimeAsia/Omsk
    Pacific SA Standard TimeAmerica/Santiago
    Pacific SA Standard TimeAmerica/Santiago
    Pacific Standard TimeAmerica/Los_Angeles
    Pacific Standard TimeAmerica/Vancouver America/Dawson America/Whitehorse
    Pacific Standard TimeAmerica/Los_Angeles America/Metlakatla
    Pacific Standard TimePST8PDT
    Pacific Standard Time (Mexico)America/Tijuana
    Pacific Standard Time (Mexico)America/Tijuana America/Santa_Isabel
    Pakistan Standard TimeAsia/Karachi
    Pakistan Standard TimeAsia/Karachi
    Paraguay Standard TimeAmerica/Asuncion
    Paraguay Standard TimeAmerica/Asuncion
    Romance Standard TimeEurope/Paris
    Romance Standard TimeEurope/Brussels
    Romance Standard TimeEurope/Copenhagen
    Romance Standard TimeEurope/Madrid Africa/Ceuta
    Romance Standard TimeEurope/Paris
    Russia Time Zone 10Asia/Srednekolymsk
    Russia Time Zone 10Asia/Srednekolymsk
    Russia Time Zone 11Asia/Kamchatka
    Russia Time Zone 11Asia/Kamchatka Asia/Anadyr
    Russia Time Zone 3Europe/Samara
    Russia Time Zone 3Europe/Samara
    Russian Standard TimeEurope/Moscow
    Russian Standard TimeEurope/Moscow Europe/Kirov Europe/Volgograd
    Russian Standard TimeEurope/Simferopol
    SA Eastern Standard TimeAmerica/Cayenne
    SA Eastern Standard TimeAntarctica/Rothera
    SA Eastern Standard TimeAmerica/Fortaleza America/Belem America/Maceio America/Recife America/Santarem
    SA Eastern Standard TimeAtlantic/Stanley
    SA Eastern Standard TimeAmerica/Cayenne
    SA Eastern Standard TimeAmerica/Paramaribo
    SA Eastern Standard TimeEtc/GMT+3
    SA Pacific Standard TimeAmerica/Bogota
    SA Pacific Standard TimeAmerica/Rio_Branco America/Eirunepe
    SA Pacific Standard TimeAmerica/Coral_Harbour
    SA Pacific Standard TimeAmerica/Bogota
    SA Pacific Standard TimeAmerica/Guayaquil
    SA Pacific Standard TimeAmerica/Jamaica
    SA Pacific Standard TimeAmerica/Cayman
    SA Pacific Standard TimeAmerica/Panama
    SA Pacific Standard TimeAmerica/Lima
    SA Pacific Standard TimeEtc/GMT+5
    SA Western Standard TimeAmerica/La_Paz
    SA Western Standard TimeAmerica/Antigua
    SA Western Standard TimeAmerica/Anguilla
    SA Western Standard TimeAmerica/Aruba
    SA Western Standard TimeAmerica/Barbados
    SA Western Standard TimeAmerica/St_Barthelemy
    SA Western Standard TimeAmerica/La_Paz
    SA Western Standard TimeAmerica/Kralendijk
    SA Western Standard TimeAmerica/Manaus America/Boa_Vista America/Porto_Velho
    SA Western Standard TimeAmerica/Blanc-Sablon
    SA Western Standard TimeAmerica/Curacao
    SA Western Standard TimeAmerica/Dominica
    SA Western Standard TimeAmerica/Santo_Domingo
    SA Western Standard TimeAmerica/Grenada
    SA Western Standard TimeAmerica/Guadeloupe
    SA Western Standard TimeAmerica/Guyana
    SA Western Standard TimeAmerica/St_Kitts
    SA Western Standard TimeAmerica/St_Lucia
    SA Western Standard TimeAmerica/Marigot
    SA Western Standard TimeAmerica/Martinique
    SA Western Standard TimeAmerica/Montserrat
    SA Western Standard TimeAmerica/Puerto_Rico
    SA Western Standard TimeAmerica/Lower_Princes
    SA Western Standard TimeAmerica/Port_of_Spain
    SA Western Standard TimeAmerica/St_Vincent
    SA Western Standard TimeAmerica/Tortola
    SA Western Standard TimeAmerica/St_Thomas
    SA Western Standard TimeEtc/GMT+4
    SE Asia Standard TimeAsia/Bangkok
    SE Asia Standard TimeAntarctica/Davis
    SE Asia Standard TimeIndian/Christmas
    SE Asia Standard TimeAsia/Jakarta Asia/Pontianak
    SE Asia Standard TimeAsia/Phnom_Penh
    SE Asia Standard TimeAsia/Vientiane
    SE Asia Standard TimeAsia/Bangkok
    SE Asia Standard TimeAsia/Saigon
    SE Asia Standard TimeEtc/GMT-7
    Saint Pierre Standard TimeAmerica/Miquelon
    Saint Pierre Standard TimeAmerica/Miquelon
    Sakhalin Standard TimeAsia/Sakhalin
    Sakhalin Standard TimeAsia/Sakhalin
    Samoa Standard TimePacific/Apia
    Samoa Standard TimePacific/Apia
    Sao Tome Standard TimeAfrica/Sao_Tome
    Sao Tome Standard TimeAfrica/Sao_Tome
    Saratov Standard TimeEurope/Saratov
    Saratov Standard TimeEurope/Saratov
    Singapore Standard TimeAsia/Singapore
    Singapore Standard TimeAsia/Brunei
    Singapore Standard TimeAsia/Makassar
    Singapore Standard TimeAsia/Kuala_Lumpur Asia/Kuching
    Singapore Standard TimeAsia/Manila
    Singapore Standard TimeAsia/Singapore
    Singapore Standard TimeEtc/GMT-8
    South Africa Standard TimeAfrica/Johannesburg
    South Africa Standard TimeAfrica/Bujumbura
    South Africa Standard TimeAfrica/Gaborone
    South Africa Standard TimeAfrica/Lubumbashi
    South Africa Standard TimeAfrica/Maseru
    South Africa Standard TimeAfrica/Blantyre
    South Africa Standard TimeAfrica/Maputo
    South Africa Standard TimeAfrica/Kigali
    South Africa Standard TimeAfrica/Mbabane
    South Africa Standard TimeAfrica/Johannesburg
    South Africa Standard TimeAfrica/Lusaka
    South Africa Standard TimeAfrica/Harare
    South Africa Standard TimeEtc/GMT-2
    Sri Lanka Standard TimeAsia/Colombo
    Sri Lanka Standard TimeAsia/Colombo
    Sudan Standard TimeAfrica/Khartoum
    Sudan Standard TimeAfrica/Khartoum
    Syria Standard TimeAsia/Damascus
    Syria Standard TimeAsia/Damascus
    Taipei Standard TimeAsia/Taipei
    Taipei Standard TimeAsia/Taipei
    Tasmania Standard TimeAustralia/Hobart
    Tasmania Standard TimeAustralia/Hobart Australia/Currie
    Tocantins Standard TimeAmerica/Araguaina
    Tocantins Standard TimeAmerica/Araguaina
    Tokyo Standard TimeAsia/Tokyo
    Tokyo Standard TimeAsia/Jayapura
    Tokyo Standard TimeAsia/Tokyo
    Tokyo Standard TimePacific/Palau
    Tokyo Standard TimeAsia/Dili
    Tokyo Standard TimeEtc/GMT-9
    Tomsk Standard TimeAsia/Tomsk
    Tomsk Standard TimeAsia/Tomsk
    Tonga Standard TimePacific/Tongatapu
    Tonga Standard TimePacific/Tongatapu
    Transbaikal Standard TimeAsia/Chita
    Transbaikal Standard TimeAsia/Chita
    Turkey Standard TimeEurope/Istanbul
    Turkey Standard TimeEurope/Istanbul
    Turks And Caicos Standard TimeAmerica/Grand_Turk
    Turks And Caicos Standard TimeAmerica/Grand_Turk
    US Eastern Standard TimeAmerica/Indianapolis
    US Eastern Standard TimeAmerica/Indianapolis America/Indiana/Marengo America/Indiana/Vevay
    US Mountain Standard TimeAmerica/Phoenix
    US Mountain Standard TimeAmerica/Dawson_Creek America/Creston America/Fort_Nelson
    US Mountain Standard TimeAmerica/Hermosillo
    US Mountain Standard TimeAmerica/Phoenix
    US Mountain Standard TimeEtc/GMT+7
    UTCEtc/GMT
    UTCAmerica/Danmarkshavn
    UTCEtc/GMT Etc/UTC
    UTC+12Etc/GMT-12
    UTC+12Pacific/Tarawa
    UTC+12Pacific/Majuro Pacific/Kwajalein
    UTC+12Pacific/Nauru
    UTC+12Pacific/Funafuti
    UTC+12Pacific/Wake
    UTC+12Pacific/Wallis
    UTC+12Etc/GMT-12
    UTC+13Etc/GMT-13
    UTC+13Pacific/Enderbury
    UTC+13Pacific/Fakaofo
    UTC+13Etc/GMT-13
    UTC-02Etc/GMT+2
    UTC-02America/Noronha
    UTC-02Atlantic/South_Georgia
    UTC-02Etc/GMT+2
    UTC-08Etc/GMT+8
    UTC-08Pacific/Pitcairn
    UTC-08Etc/GMT+8
    UTC-09Etc/GMT+9
    UTC-09Pacific/Gambier
    UTC-09Etc/GMT+9
    UTC-11Etc/GMT+11
    UTC-11Pacific/Pago_Pago
    UTC-11Pacific/Niue
    UTC-11Pacific/Midway
    UTC-11Etc/GMT+11
    Ulaanbaatar Standard TimeAsia/Ulaanbaatar
    Ulaanbaatar Standard TimeAsia/Ulaanbaatar Asia/Choibalsan
    Venezuela Standard TimeAmerica/Caracas
    Venezuela Standard TimeAmerica/Caracas
    Vladivostok Standard TimeAsia/Vladivostok
    Vladivostok Standard TimeAsia/Vladivostok Asia/Ust-Nera
    W. Australia Standard TimeAustralia/Perth
    W. Australia Standard TimeAntarctica/Casey
    W. Australia Standard TimeAustralia/Perth
    W. Central Africa Standard TimeAfrica/Lagos
    W. Central Africa Standard TimeAfrica/Luanda
    W. Central Africa Standard TimeAfrica/Porto-Novo
    W. Central Africa Standard TimeAfrica/Kinshasa
    W. Central Africa Standard TimeAfrica/Bangui
    W. Central Africa Standard TimeAfrica/Brazzaville
    W. Central Africa Standard TimeAfrica/Douala
    W. Central Africa Standard TimeAfrica/Algiers
    W. Central Africa Standard TimeAfrica/Libreville
    W. Central Africa Standard TimeAfrica/Malabo
    W. Central Africa Standard TimeAfrica/Niamey
    W. Central Africa Standard TimeAfrica/Lagos
    W. Central Africa Standard TimeAfrica/Ndjamena
    W. Central Africa Standard TimeAfrica/Tunis
    W. Central Africa Standard TimeEtc/GMT-1
    W. Europe Standard TimeEurope/Berlin
    W. Europe Standard TimeEurope/Andorra
    W. Europe Standard TimeEurope/Vienna
    W. Europe Standard TimeEurope/Zurich
    W. Europe Standard TimeEurope/Berlin Europe/Busingen
    W. Europe Standard TimeEurope/Gibraltar
    W. Europe Standard TimeEurope/Rome
    W. Europe Standard TimeEurope/Vaduz
    W. Europe Standard TimeEurope/Luxembourg
    W. Europe Standard TimeEurope/Monaco
    W. Europe Standard TimeEurope/Malta
    W. Europe Standard TimeEurope/Amsterdam
    W. Europe Standard TimeEurope/Oslo
    W. Europe Standard TimeEurope/Stockholm
    W. Europe Standard TimeArctic/Longyearbyen
    W. Europe Standard TimeEurope/San_Marino
    W. Europe Standard TimeEurope/Vatican
    W. Mongolia Standard TimeAsia/Hovd
    W. Mongolia Standard TimeAsia/Hovd
    West Asia Standard TimeAsia/Tashkent
    West Asia Standard TimeAntarctica/Mawson
    West Asia Standard TimeAsia/Oral Asia/Aqtau Asia/Aqtobe Asia/Atyrau Asia/Qyzylorda
    West Asia Standard TimeIndian/Maldives
    West Asia Standard TimeIndian/Kerguelen
    West Asia Standard TimeAsia/Dushanbe
    West Asia Standard TimeAsia/Ashgabat
    West Asia Standard TimeAsia/Tashkent Asia/Samarkand
    West Asia Standard TimeEtc/GMT-5
    West Bank Standard TimeAsia/Hebron
    West Bank Standard TimeAsia/Hebron Asia/Gaza
    West Pacific Standard TimePacific/Port_Moresby
    West Pacific Standard TimeAntarctica/DumontDUrville
    West Pacific Standard TimePacific/Truk
    West Pacific Standard TimePacific/Guam
    West Pacific Standard TimePacific/Saipan
    West Pacific Standard TimePacific/Port_Moresby
    West Pacific Standard TimeEtc/GMT-10
    Yakutsk Standard TimeAsia/Yakutsk
    Yakutsk Standard TimeAsia/Yakutsk Asia/Khandyga
  2. 實際使用

    TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("Asia/Shanghai");
    var utcNow = DateTime.UtcNow;
    Console.WriteLine(utcNow);
    Console.WriteLine(TimeZoneInfo.ConvertTime(utcNow, timeZone));
    
  3. 通吃雙平台

    TimeZoneInfo timeZone;
    try {
        timeZone = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time");
    }
    catch (Exception ex)
    {
        timeZone = TimeZoneInfo.FindSystemTimeZoneById("Asia/Shanghai");
    }
    
    var utcNow = DateTime.UtcNow;
    Console.WriteLine(utcNow);
    Console.WriteLine(TimeZoneInfo.ConvertTime(utcNow, timeZone));
    

心得

解法 1 : 使用套件 比較方便,缺點是對套件有相依,需要多管理成本

解法 2 : 使用 TZ database name 沒有套件相依問題,但跨平台應用的寫法上可能需要調整,try catch 很方便,不過效能的耗損在頻繁使用下是無法忽略

參考資訊

  1. mj1856/TimeZoneConverter
  2. FindSystemTimeZoneById doesn’t support “Eastern Standard Time” on Ubuntu
  3. List of tz database time zones
  4. Zone → Tzid