nall got me in the right direction. I slapped together a category on NSDate that takes care of the details and is both 10.5/10.6 friendly and uses no Carbon. Thanks for the help, nall!
NSDate-Additions.h
#import <Foundation/Foundation.h>
@interface NSDate (NSDate_Additions)
+(NSTimeInterval) timeIntervalSinceSystemStartup;
-(NSTimeInterval) timeIntervalSinceSystemStartup;
+(NSDate *) dateOfSystemStartup;
+(NSDate *) dateWithCGEventTimestamp:(CGEventTimestamp)cgTimestamp;
@end
NSDate-Additions.m
#import "NSDate-Additions.h"
#include <assert.h>
#include <CoreServices/CoreServices.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <unistd.h>
#if MAC_OS_X_VERSION_MAX_ALLOWED == MAC_OS_X_VERSION_10_5
@interface NSProcessInfo (SnowLeopard)
- (NSTimeInterval)systemUptime;
@end
@interface NSDate (SnowLeopard)
- (id)dateByAddingTimeInterval:(NSTimeInterval)seconds;
@end
#endif
// Boosted from Apple sample code
uint64_t UpTimeInNanoseconds(void)
{
uint64_t time;
uint64_t timeNano;
static mach_timebase_info_data_t sTimebaseInfo;
time = mach_absolute_time();
// Convert to nanoseconds.
// If this is the first time we've run, get the timebase.
// We can use denom == 0 to indicate that sTimebaseInfo is
// uninitialised because it makes no sense to have a zero
// denominator is a fraction.
if ( sTimebaseInfo.denom == 0 ) {
(void) mach_timebase_info(&sTimebaseInfo);
}
// Do the maths. We hope that the multiplication doesn't
// overflow; the price you pay for working in fixed point.
timeNano = time * sTimebaseInfo.numer / sTimebaseInfo.denom;
return timeNano;
}
@implementation NSDate (NSDate_Additions)
+(NSTimeInterval) timeIntervalSinceSystemStartup
{
NSTimeInterval interval;
long sysVersion;
Gestalt( gestaltSystemVersion, &sysVersion );
if( sysVersion >= 0x1060 )
interval = [[NSProcessInfo processInfo] systemUptime];
else
interval = UpTimeInNanoseconds() / 1000000000.0;
return( interval );
}
-(NSTimeInterval) timeIntervalSinceSystemStartup
{
return( [self timeIntervalSinceDate:[NSDate dateOfSystemStartup]] );
}
+(NSDate *) dateOfSystemStartup
{
return( [NSDate dateWithTimeIntervalSinceNow:-([NSDate timeIntervalSinceSystemStartup])] );
}
+(NSDate *) dateWithCGEventTimestamp:(CGEventTimestamp)cgTimestamp
{
NSDate *ssuDate = nil;
NSDate *cgDate = nil;
long sysVersion;
ssuDate = [NSDate dateOfSystemStartup];
Gestalt( gestaltSystemVersion, &sysVersion );
if( sysVersion >= 0x1060 )
cgDate = [ssuDate dateByAddingTimeInterval:(cgTimestamp/1000000000.0)];
else
cgDate = [ssuDate addTimeInterval:(cgTimestamp/1000000000.0)];
return( cgDate );
}
@end