Created
May 24, 2024 18:02
-
-
Save robertmryan/98669fcb526c37e449551521e90f91d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: NSViewController { | |
let calendar = Calendar.autoupdatingCurrent | |
var previousIdentifier: String = Calendar.current.timeZone.identifier | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// poll to see if the identifier changed | |
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] timer in | |
guard let self else { | |
timer.invalidate() | |
return | |
} | |
let identifier = calendar.timeZone.identifier | |
if previousIdentifier != identifier { | |
print("Time zone changed from", previousIdentifier, "to", identifier) | |
previousIdentifier = identifier | |
} | |
} | |
// better, let the OS tell us when the time zone changes | |
NotificationCenter.default.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: .main) { [weak self] _ in | |
guard let self else { | |
return | |
} | |
print("Time zone changed to", calendar.timeZone.identifier) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah, that is how I would have to do it. The reason it's ugly is that it brings in this pattern which is not required on the other platforms that the application targets and complicates the logic for termination control.
I agree that you've shown that it does what's expected and thank you for digging into what is necessary to get it to work in this context.