I have a Cocoa application that records datestamps on events. I need to know when the system time is reset and by how much, but I can’t seem to fine a notification anywhere that tells me such a thing has happened. This change could happen because NTP reset the clock or because the user reset (e.g. from System Preferences). It would be great if there was just a NSNotification I could register to receive, but I’m open to any suggestions.
I have a Cocoa application that records datestamps on events. I need to know
Share
Apple added in NSSystemClockDidChangeNotification, part of NSDate, in Snow Leopard (10.6). There doesn’t appear to be a way to do it in Leopard (10.5) or earlier. Per the Apple NSDate docs:
This doesn’t appear to tell you “how much” time has changed. You could possibly calculate that by periodically (say, every 5 seconds in a
NSTimer) capturing the system time with[NSDate date], saving it into a variable, and then afterNSSystemClockDidChangeNotificationfires, grab the new date and compare the two together using NSDate’stimeIntervalSinceDate:method to get the difference.Not millisecond or even second accurate, but pretty close.
EDIT: See this post. You could possibly use the
UpTime()C command to grab the system uptime in CPU tics (which you can later convert to seconds). You could use this to figure out by how much time has changed (assuming no system restart or sleep). This works even if the system clock is changed by the user or network time protocol.