[英]TimeZoneInfo bug in .net 4.5
I have recently updated my os with .net 4.5 framework and compiled all my applications using it. 我最近用.net 4.5框架更新了操作系统,并使用它编译了所有应用程序。 unfortunately some of the automatic tests i wrote now fails on the Assert constructions concerning DateTime types. 不幸的是,我现在编写的一些自动测试在有关DateTime类型的Assert构造上失败了。
After a deep analysis i turned out this: 经过深入分析,我发现:
in .net 4.0 在.net 4.0中
DateTime dateUsing40 = new DateTime(2011, 4, 7); // ticks 634377312000000000
dateUsing40.ToUniversalTime(); //ticks **634377240000000000
bool isDaylightST = dateUsing40.IsDaylightSavingTime(); // returns **true
in .net 4.5 在.net 4.5中
DateTime dateUsing45 = new DateTime(2011, 4, 7); // ticks 634377312000000000
dateUsing45.ToUniversalTime(); //ticks **634377276000000000
bool isDaylightST = dateUsing45.IsDaylightSavingTime(); // returns **false
the System.Threading.Thread.CurrentThread.CurrentCulture is in both cases {it-IT} 在两种情况下,System.Threading.Thread.CurrentThread.CurrentCulture均为{it-IT}
Actually the date i used is in the range of the (italian but also for all countries that use WET) daylight time so it looks like there is a (huge) bug in the framework. 实际上,我使用的日期在(意大利语,也对于使用WET的所有国家/地区)日光时间范围内,因此看起来框架中存在一个(巨大的)错误。 however i didn't find anything useful about. 但是我没有发现任何有用的东西。
I verified in both machines: 我在两台机器上都进行了验证:
SOLVED: 解决了:
the framework update changed the DynamicDaylightTimeDisabled's value to 1. To solve this issue it's necessary to turn it to 0 and rebooting. 框架更新将DynamicDaylightTimeDisabled的值更改为1。要解决此问题,必须将其设置为0并重新启动。 Another way to do it is using the clock UI form. 另一种方法是使用时钟UI表单。
Check the value of var tziLocal = TimeZoneInfo.Local;
检查var tziLocal = TimeZoneInfo.Local;
的值var tziLocal = TimeZoneInfo.Local;
(cf. Shaun's answer), and its Id
property. (请参阅Shaun的答案)及其Id
属性。
Equivalently (I think), go to PowerShell and write: 同样(我认为),转到PowerShell并编写:
Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\TimeZoneInformation -Name TimeZoneKeyName
and check the value of TimeZoneKeyName
winreg "property". 并检查TimeZoneKeyName
winreg“ property”的值。 Get-ItemProperty
can be shortened to just gp
. 可以将Get-ItemProperty
缩短为gp
。
Based on experimentation, the CurrentCulture
, CurrentUICulture
and RegionInfo.CurrentRegion
are not used, for determining "local time". 根据实验, 不使用CurrentCulture
, CurrentUICulture
和RegionInfo.CurrentRegion
来确定“本地时间”。
Instead if you go to Windows, Control Panel → Clock, Language, and Region → Date and Time → tab Date and Time → section Time zone → button Change time zone... , changing that zone seems to work. 相反,如果您转到Windows,请依次选择控制面板→时钟,语言和区域→日期和时间→选项卡日期和时间→时区→按钮更改时区... ,更改该时区似乎是可行的。
Of course, if Windows registry contains wrong settings for Italy, typically the ID "W. Europe Standard Time"
(not "Romance Standard Time"
, not "Central European Standard Time"
), on your particular machine, that would be a problem. 当然,如果Windows注册表中包含错误的意大利设置,通常是特定计算机上的ID "W. Europe Standard Time"
(不是"Romance Standard Time"
,而不是"Central European Standard Time"
),那将是一个问题。
How about using the TimeZoneInfo class? 如何使用TimeZoneInfo类?
DateTime dateUsing45 = new DateTime(2011, 4, 7); // ticks 634377312000000000
dateUsing45.ToUniversalTime(); //ticks **634377276000000000
TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
var isDaylightST = cstZone.IsDaylightSavingTime(dateUsing45);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.