Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binding request: NSSystemTimeZoneDidChangeNotification & NSSystemClockDidChangeNotification #19837

Closed
tipa opened this issue Jan 16, 2024 · 0 comments · Fixed by #19846
Closed

Comments

@tipa
Copy link

tipa commented Jan 16, 2024

I have recently found out that the time zone information is cached and calls to DateTime.Now and similar APIs use the wrong (old) time zone after the time zone has been changed in the system settings. In order to retrieve the correct local time, it is required to listen to a time zone changed system event and then call TimeZoneInfo.ClearCachedData();
dotnet/runtime#97010

In order to do that, I wanted to implement a method like this:

static NSObject timeZoneObserver;
public static void SetupTimeZoneChangedListener()
{
    timeZoneObserver = NSNotificationCenter.DefaultCenter.AddObserver(NSSystemTimeZoneDidChangeNotification, n =>
    {
          TimeZoneInfo.ClearCachedData();
    });
}

However, the NSSystemTimeZoneDidChangeNotification variable is not bound. Neither is NSSystemClockDidChangeNotification

As a workaround, I inspected the variable name in Xcode and I am able to use this workaround:

timeZoneObserver = NSNotificationCenter.DefaultCenter.AddObserver(new NSString("kCFTimeZoneSystemTimeZoneDidChangeNotification"), n =>
{
    TimeZoneInfo.ClearCachedData();
});

Environment

.NET 8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants