[mscorlib/Android] CultureInfo.ClearCachedData() clears DateTime.
authorJonathan Pryor <jonpryor@vt.edu>
Fri, 12 Dec 2014 17:17:53 +0000 (12:17 -0500)
committerJonathan Pryor <jonpryor@vt.edu>
Fri, 12 Dec 2014 17:29:04 +0000 (12:29 -0500)
commitade02c3f670a9e9d590d0a30b389c1bfe8e6796b
treee86a688fe4cc4e866815c99806be7a371e4ffae0
parent8f21c92ad9e0cb8ddae58efe64c03767f5ccd4ac
[mscorlib/Android] CultureInfo.ClearCachedData() clears DateTime.

Context: https://bugzilla.xamarin.com/show_bug.cgi?id=24947

When an Android user changes the timezone, those TimeZone changes
aren't reflected in Xamarin.Android apps. Part of this is a
Xamarin.Android bug, in which it needs to listen for TimeZone change
notifications and in turn let Mono know that the TimeZone changed.

Xamarin.Android can/will call Thread.CurrentCulture.ClearCachedData()
and Thread.CurrentUICulture.ClearCachedData() when the TimeZone has
changed. This will allow Mono to lookup the TimeZone info again.

At which point we hit two problems within Mono:

 1. TimeZoneInfo.AndroidTimeZones.Local is unnecessarily cached.
 2. DateTime itself caches the UTC offset for use by DateTime.Now.

TimeZoneInfo.Local is *already* cached, and TimeZoneInfo.Local is
properly cleared by TimeZoneInfo.ClearCachedData() while
TimeZoneInfo.AndroidTimeZones.Local wasn't. Removing the extra
cache allows the TimeZoneInfo to be looked up again.

Which brings us to DateTime.Now: for my simple test case, after
calling CultureInfo.ClearCachedData() the result of
DateTime.Now.ToString() didn't reflect the newly current TimeZone. The
cause for this was DateTime.to_local_time_span_object and
DateTime.last_now, which DateTime.Now uses as a cache for the
TimeZone's UTC offset.

Since these DateTime fields weren't cleared, subsequent DateTime.Now
invocations reported the *previous* TimeZone instead of the current
timezone.

To fix this, add an `internal` DateTime.ClearCachedData() method, and
call DateTime.ClearCachedData() from CultureInfo.ClearCachedData().
mcs/class/System.Core/System/TimeZoneInfo.Android.cs
mcs/class/corlib/System.Globalization/CultureInfo.cs
mcs/class/corlib/System/DateTime.cs