Binding request: NSSystemTimeZoneDidChangeNotification & NSSystemClockDidChangeNotification · Issue #19837 · dotnet/macios (original) (raw)
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