TimeZoneInfo error?
I'm having problems converting time from Lo开发者_StackOverflow社区ndon (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?
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.
精彩评论