How can I get NodaTime to update when Android updates the TimeZone?

91 Views Asked by At

I'm new to NodaTime & still finding my way around it.

If when I start my program the Android timezone is set to "America/New York" NodaTime DateTimeZoneProviders.Tzdb.GetSystemDefault() correctly returns "America/New_York"

However if I then update the timezone via Android settings to "America/Denver" while my program is running, then DateTimeZoneProviders.Tzdb.GetSystemDefault() still returns "America/New_York"

How do I get the program to read the new Android System timezone & return that information?

I can't see any equivalent of TimeZoneInfo.ClearCachedData() and running that first makes no difference to NodaTime, although both TimeZoneInfo & DateTime.Now are correctly updated

1

There are 1 best solutions below

3
Jon Skeet On

TzdbDateTimeZoneSource doesn't do any caching - it fetches TimeZoneInfo.Local.Id and maps that.

So I would expect calling TimeZoneInfo.ClearCachedData() to be sufficient, so long as that updates TimeZoneInfo.Local.Id.

In other words, if you run:

Log(TimeZoneInfo.Local.Id);
Log(DateTimeZoneProviders.Tzdb.GetSystemDefault().Id);

// Pause and adjust the system default time zone.
// Then:

TimeZoneInfo.ClearCachedData();
Log(TimeZoneInfo.Local.Id);
Log(DateTimeZoneProviders.Tzdb.GetSystemDefault().Id);

... I'd expect to see consistency, e.g. "America/New_York" twice and then "America/Denver" twice.

If you see "America/New_York" four times, that means TimeZoneInfo.ClearCachedData() isn't actually updating the local time zone ID.

If you see "America/New_York" twice, then "America/Denver", then "America/New_York", then that would be a bug in Noda Time - but assuming you're using a recent version, I can't easily see how that would happen. (DateTimeZoneCache doesn't cache the results of GetSystemDefault(), nor does TzdbDateTimeZoneSource.)