Reputation: 1520
I have recently updated my os with .net 4.5 framework and compiled all my applications using it. unfortunately some of the automatic tests i wrote now fails on the Assert constructions concerning DateTime types.
After a deep analysis i turned out this:
in .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
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}
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. 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. Another way to do it is using the clock UI form.
Upvotes: 3
Views: 850
Reputation: 61912
Check the value of var tziLocal = TimeZoneInfo.Local;
(cf. Shaun's answer), and its Id
property.
Equivalently (I think), go to PowerShell and write:
Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\TimeZoneInformation -Name TimeZoneKeyName
and check the value of TimeZoneKeyName
winreg "property". Get-ItemProperty
can be shortened to just gp
.
Based on experimentation, the CurrentCulture
, CurrentUICulture
and RegionInfo.CurrentRegion
are not used, for determining "local time".
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.
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.
Upvotes: 2
Reputation: 8358
How about using the TimeZoneInfo class?
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);
Upvotes: 2