0

When I change the time zone on my computer (when testing on the simulator) or on the phone (when testing directly on the phone), DateTime.ToLocalTime() does not return updated results.

I register and successfully receive notification events for UIApplication.Notifications.ObserveSignificantTimeChange. In it I call TimeZoneInfo.ClearCachedData() which allows me to detect the time zone has in fact changed but this has no effect on ToLocalTime(). The DateTime I am converting is definitely of kind Utc (tried DateTime.UtcNow for example).

I tried calling CultureInfo.CurrentCulture.ClearCachedData() as suggested in this question: SetTimeZoneInformation does not update DateTime.Now for another .NET Application. That only causes the application to crash when the CurrentCulture is nulled.

Digging a bit deeper into the implementation of ToLocalTime(), it seems to use the object TimeZone.CurrentTimeZone. This object is reset only when the difference between DateTime.GetNow() and TimeZone.timezone_check. I suspect that TimeZone.timezone_check is not getting updated when TimeZoneInfo.ClearCachedData() is called.

Is there any way I can force ToLocalTime() to take into consideration the time zone change?

Community
  • 1
  • 1
ysalmi
  • 529
  • 4
  • 16

1 Answers1

2

You can use DateTimeOffset for this. See also this question.

Edit - What I have working is this:

var dateTimeUtc = new System.DateTime(2013, 2, 4, 21, 30, 0, 
                                        System.DateTimeKind.Utc);
var dateTimeOffsetLocal = new System.DateTimeOffset(dateTimeUtc.ToLocalTime());
var formattedLocalTime = string.Format("{0:HH:mm}", dateTimeOffsetLocal);

On my phone my formattedLocalTime changes whenever I change my TimeZone on it, without clearing any cache. I do have to reload the view, but no other tricks.

To display/verify the local TimeZone I use:

var localTimeZone = System.TimeZoneInfo.Local.StandardName;

I hope this somehow points you in the right direction. I don't have any better than this "works on my machine(s)"-anwer.

Community
  • 1
  • 1
Jacco
  • 3,251
  • 1
  • 19
  • 29
  • Good suggestion, but I run into the same problem. Calling `DateTimeOffset.ToLocalTime()` still makes use of `TimeZone.CurrentTimeZone` which does not seem to get updated. – ysalmi Feb 04 '13 at 20:41
  • I'll have to look into my own implementation. This one is working for me. Hoop to get back on this. – Jacco Feb 04 '13 at 20:42
  • Thanks for your help. I feel like I'm missing something. I tried `TimeZoneInfo.ConvertTimeFromUtc(myUTCDate, TimeZoneInfo.Local)` which uses an up to date TimeZoneInfo, but I am still getting the wrong local time when I print out the result. – ysalmi Feb 04 '13 at 21:04
  • I expanded my answer with a sample from my own code. Hope it helps. – Jacco Feb 04 '13 at 21:48
  • Thanks for your answer Jacco, I am still looking deeper into this, I'm pretty sure there's a bug on my side. Just to be clear, this works for you on the iOS simulator? – ysalmi Feb 05 '13 at 19:04