[英]How can I get notified of a system time change in my Cocoa application?
I have a Cocoa application that records datestamps on events. 我有一个Cocoa应用程序,记录事件的日期戳。 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 (eg from System Preferences).
这种变化可能发生,因为NTP重置时钟或因为用户重置(例如从系统偏好设置)。 It would be great if there was just a
NSNotification
I could register to receive, but I'm open to any suggestions. 如果只有我可以注册接收的
NSNotification
会很棒,但我愿意接受任何建议。
Apple added in NSSystemClockDidChangeNotification , part of NSDate, in Snow Leopard (10.6). Apple在Snow Leopard(10.6)的NSSateClockDidChangeNotification中加入了NSDate的一部分。 There doesn't appear to be a way to do it in Leopard (10.5) or earlier.
似乎没有办法在Leopard(10.5)或更早版本中执行此操作。 Per the Apple NSDate docs :
根据Apple NSDate文档 :
Posted whenever the system clock is changed.
系统时钟更改时发布。 This can be initiated by a call to settimeofday() or the user changing values in the Date and Time Preference panel.
这可以通过调用settimeofday()或用户更改“日期和时间首选项”面板中的值来启动。 The notification object is null.
通知对象为null。 This notification does not contain a userInfo dictionary.
此通知不包含userInfo字典。
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 after NSSystemClockDidChangeNotification
fires, grab the new date and compare the two together using NSDate's timeIntervalSinceDate:
method to get the difference. 您可以通过定期(例如,在
NSTimer
每隔5秒)使用[NSDate date]
捕获系统时间,将其保存到变量中,然后在NSSystemClockDidChangeNotification
触发后,抓住新日期并使用NSDate将两者进行比较来计算timeIntervalSinceDate:
获取差异的方法。
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). 您可以使用
UpTime()
C命令来获取CPU tics中的系统正常运行时间(稍后可以将其转换为秒)。 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.
即使系统时钟被用户或网络时间协议更改,这也可以工作。
If some on is looking for solution know system date change change event from 10.4 如果有人正在寻找解决方案,请知道系统日期更改事件从10.4开始
OSStatus DateChangeEventHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData)
{
NSLog(@"Event received!\n");
return 0;
}
- (void)SystemTimeChangeHandler
{
EventTypeSpec eventType;
eventType.eventClass = kEventClassSystem;
eventType.eventKind = kEventSystemTimeDateChanged;
EventHandlerUPP eventHandlerUPP =
NewEventHandlerUPP(DateChangeEventHandler);
EventHandlerRef eventHandlerRef = NULL;
(void)InstallApplicationEventHandler(
eventHandlerUPP,
1,
&eventType,
self,
&eventHandlerRef);
}
Time moves constantly. 时间不断变化。 A notification every time the current time changed would be a constant, CPU-soaking stream of notifications.
每当当前时间改变时的通知将是一个恒定的,CPU浸泡的通知流。
What you need to do is get the current time in your event handler—the one that receives the events you're datestamping. 您需要做的是获取事件处理程序中的当前时间 - 接收您正在进行日期戳记的事件的时间。 You get the current time by calling
[NSDate date]
. 您可以通过调用
[NSDate date]
获取当前时间。
I don't think there's a single way to do that because of the different mechanisms by which the time could change. 我认为没有一种方法可以做到这一点,因为时间可以改变的机制不同。 But it wouldn't be very expensive (too expensive? Don't know, have you profiled it yet? ;-) to set an
NSTimer
once a second to check the time and compare it with the previous value. 但它不会非常昂贵(太贵了?不知道,你有没有对它进行分析?;-)每秒设置一次
NSTimer
以检查时间并将其与之前的值进行比较。 If it's not advanced by about a second, something interesting happened and you can notify your audit object. 如果它没有提前约一秒钟,就会发生一些有趣的事情,你可以通知你的审计对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.