時區信息錯誤? (TimeZoneInfo error?)


問題描述

時區信息錯誤? (TimeZoneInfo error?)

I'm having problems converting time from London (UTC/UTC+1) to US Eastern Time (UTC‑5/UTC‑4).

With code running in London machine during its UTC+1 phase, 

TimeZoneInfo.ConvertTimeFromUtc(DateTime.Now.ToUniversalTime(), TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"));

This will correctly convert 14:25:00 to 06:25:00 from UTC+1 to UTC‑7

However, if the time zone is US Eastern

TimeZoneInfo.ConvertTimeFromUtc(DateTime.Now.ToUniversalTime(), TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time"));

This will incorrectly convert 14:25:00 to 08:25:00 from UTC+1 to UTC‑4

In addition, 

TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time").SupportsDaylightSavingTime

returns false while

TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time").SupportsDaylightSavingTime

returns true.

This is running under .NET 3.5. Any thoughts?

‑‑‑‑‑

參考解法

方法 1:

It really seems like a bug, either faulty timezone info on the platform you are running .NET on; or in .NET itself. In my mind you should get true for Eastern Standard Time.

If you would use the tz info database I am pretty sure you would get less ambiguous results. Check out: http://www.codeproject.com/KB/dotnet/Using_time_zones_in_NET.aspx.

And learn more about the open timezone database here: http://en.wikipedia.org/wiki/Tz_database


Further thoughts: It is hard to say what is the bug here. Take for example Eastern Standard Time. It is by definition the winter time for the eastern seaboard, in summer Eastern Standard Time does not really exists. Suddenly that region starts using "Eastern Daylight Time".

The same goes for central europe which alternates between "Central Europe Time" And "Central Europe Standard Time". The conventions around the namings of the timezones are really confusing, and also many times ambiguous. I can only repeat that if you can find a reliable tz database API for your platform and programming language: use it.

(by Jiang QiuJon Nylander)

參考文件

  1. TimeZoneInfo error? (CC BY‑SA 3.0/4.0)

#.net-3.5 #datetime #timezone






相關問題

如何在 C# 中搜索 pdf 中的文本(執行匹配) (How to search text (Exect match) in pdf in C#)

String.IsNullOrEmpty() (String.IsNullOrEmpty())

是否有可以綁定的可為空的日期選擇器? (Is there a nullable datepicker that I can bind to?)

具有自動命名屬性的通用組合框 (Generic ComboBox with automatically named properties)

在數據庫未定義的外鍵關係上配置實體框架 (Configuring Entity Framework on a Database Undefined Foreign Key Relationships)

我可以在 4.0 應用程序中引用 .NET 3.5 .DLL 嗎? (Can I reference a .NET 3.5 .DLL in a 4.0 app?)

如何在 ASP.NET、VB.NET 中解決這個會話問題? (How to tackle this session problem in ASP.NET,VB.NET?)

在 ADO.NET 數據服務中添加對查找表的引用 (Adding a reference to a lookup table in ADO.NET Data Services)

如何優化 Linq to Xml 查詢反對屬性? (How do I optimize a Linq to Xml query againist attributes?)

VS2005 和 LINQ (VS2005 and LINQ)

時區信息錯誤? (TimeZoneInfo error?)

與標準 C++ 相比,C++/CLI(以前稱為“託管 C++”)有哪些優勢? (What are the advantages of C++/CLI (formerly "Managed C++") over standard C++?)







留言討論