I’m writing a Cocoa OS X (Leopard 10.5+) end-user program that’s using timestamps to calculate statistics for how long something is being displayed on the screen. Time is calculated periodically while the program runs using a repeating NSTimer. [NSDate date] is used to capture timestamps, Start and Finish. Calculating the difference between the two dates in seconds is trivial.
A problem occurs if an end-user or ntp changes the system clock. [NSDate date] relies on the system clock, so if it’s changed, the Finish variable will be skewed relative to the Start, messing up the time calculation significantly. My question:
1. How can I accurately calculate the time between Start and Finish, in seconds, even when the system clock is changed mid-way?
I’m thinking that I need a non-changing reference point in time so I can calculate how many seconds has passed since then. For example, system uptime. 10.6 has - (NSTimeInterval)systemUptime, part of NSProcessInfo, which provides system uptime. However, this won’t work as my app must work in 10.5.
I’ve tried creating a time counter using NSTimer, but this isn’t accurate. NSTimer has several different run modes and can only run one at a time. NSTimer (by default) is put into the default run mode. If a user starts manipulating the UI for a long enough time, this will enter NSEventTrackingRunLoopMode and skip over the default run mode, which can lead to NSTimer firings being skipped, making it an inaccurate way of counting seconds.
I’ve also thought about creating a separate thread (NSRunLoop) to run a NSTimer second-counter, keeping it away from UI interactions. But I’m very new to multi-threading and I’d like to stay away from that if possible. Also, I’m not sure if this would work accurately in the event the CPU gets pegged by another application (Photoshop rendering a large image, etc…), causing my NSRunLoop to be put on hold for long enough to mess up its NSTimer.
I appreciate any help. 🙂
I figured out a way to do this using the UpTime() C function, provided in
<CoreServices/CoreServices.h>. This returns Absolute Time (CPU-specific), which can easily be converted into Duration Time (milliseconds, or nanoseconds). Details here: http://www.meandmark.com/timingpart1.html (look under part 3 for UpTime)I couldn’t get
mach_absolute_time()to work properly, likely due to my lack of knowledge on it, and not being able to find much documentation on the web about it. It appears to grab the same time asUpTime(), but converting it into a double left me dumbfounded.[[NSApp currentEvent] timestamp]did work, but only if the application was receiving NSEvents. If the application went into the foreground, it wouldn’t receive events, and[[NSApp currentEvent] timestamp]would simply continue to return the same old timestamp again and again in an NSTimer firing method, until the end-user decided to interact with the app again.Thanks for all your help Marc and Mike! You both definitely sent me in the right direction leading to the answer. 🙂