简体   繁体   English

如何在Cocoa应用程序中收到系统时间更改的通知?

[英]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.

相关问题 如何在WebView中通知我的Cocoa应用程序onClick事件? - How can my Cocoa application be notified of onClick events in a WebView? 在OS X的Safari中更改/加载URL时如何通知可可应用程序? - How can a Cocoa application get notified when a URL is changed/loaded in Safari for OS X? 在iPhone应用程序中,当一天中的特定时间到达时,如何获得通知? - How can I get notified when it reaches a certain time of day in an iPhone application? 我如何在可可中获取应用程序菜单 - How i can get the Application Menu in Cocoa 当用户清空垃圾箱时,如何通知我的Cocoa应用程序? - How can my Cocoa app be notified when the user empties the trash? 在 macOS 的 Cocoa 应用程序中,是否可以在选择更改期间收到通知,而不仅仅是在更改结束时收到通知? - In a Cocoa Application for macOS, is it possible to get notified during a selection change and not only at the end of the change? 如何在可可应用程序中获取窗口标题? - How can i get title of window in cocoa application? 在适用于macOS的Cocoa应用程序中,我使用NSFileWrapper将捆绑包保存到磁盘。 如何使取景器为我的捆绑包显示正确的图标? - In a Cocoa Application for macOS, I save a bundle to disk using NSFileWrapper. How can I get the finder show the correct icon for my bundle? 如果某个应用程序仍在使用某个卷,您如何获得通知? - How can you get notified, if a volume is still used by an application? 在可可中,如何使用NSTimeZone以字符串形式获取系统时区偏移量? - In Cocoa, how do I use NSTimeZone to get the system time zone offset as a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM