DateTime time zone doesn’t change (cached) for process during runtime

If your application uses clients time for some processing, it should be taken into account that time zone may be cached. You should expect the following code to return first (cached) time zone after you change it during application runtime, not the freshly applied one:

var str = DateTime.Now.ToString("HH:mm:ss.ffff zzz");

So on first call of this code value for time zone will be cached and you should manually reset cache. You would have to call System.Globalization.CultureInfo.CurrentCulture.ClearCachedData() to reset it. For .NET 3.5 TimeZoneInfo class there’s its own ClearCachedData() method.

Also culture data is cached for a reason and you shouldn’t clear it too frequently, so you can find appropriate event for it. As long as actual time zone is critical for my application I’m subscribing to SystemEvents.TimeChanged event on application start:

SystemEvents.TimeChanged += (sender, args) =>
    System.Globalization.CultureInfo.CurrentCulture.ClearCachedData();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s