Tony_Henrich
Tony_Henrich

Reputation: 44075

Changing timezone in Windows is not reflected in C# TimeZone calls

On Windows 7. I am in PST TimeZone. I changed the timezone in Windows to Eastern and then executed this in LinqPad:

 TimeZoneInfo tzinfo = TimeZoneInfo.Local;
 tzinfo.Dump();

 TimeZone localZone = TimeZone.CurrentTimeZone;
  localZone.Dump();

enter image description here

enter image description here

Why is it not showing Eastern timezone and why one structure is showing Pacific timezone and the other Eastern?

Upvotes: 0

Views: 353

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241420

.NET Caches the local time zone. If you need to be sure that you account for changes the user may have made, you need to call TimeZoneInfo.ClearCachedData() before getting the local time zone.

From the MSDN docs (emphasis mine):

Cached time zone data includes data on the local time zone, the Coordinated Universal Time (UTC) zone, and any time zones that are retrieved by using methods such as FindSystemTimeZoneById and GetSystemTimeZones.

You might call the ClearCachedData method to reduce the memory devoted to the application's cache of time zone information or to reflect the fact that the local system's time zone has changed.

This probably applies within LinqPad also.

Upvotes: 1

Related Questions