我只是尝试将CGEventTimestamp (它是一个无符号的64位整数,大致表示系统启动后的纳秒)转换为NSDate。我知道有一种方法可以将“自系统启动以来的时间”转换为NSTimeInterval或相对于参考日期的日期,但我找不到它。
如何将CGEventTimestamp转换为NSDate可以接受的内容?谢谢。
(忘了说,需要10.5友好,如果可能的话,避免碳排放)
发布于 2009-10-20 21:42:25
// Determine time in NSTimeInterval seconds this event took place after system start
GCEventTimestamp nanoseconds = GetIt();
NSTimeInterval seconds = (NSTimeInterval)nanoseconds / 1000000000.0;
// GetCurrentEventTime() gives you time in seconds since system start
EventTime currentTimeInSeconds = GetCurrentEventTime();
// Given this you can figure out the date of system start, and then add
// your event time
NSDate* startTime = [NSDate dateWithTimeIntervalSinceNow:-currentTimeInSeconds];
NSDate* eventTime = [startTime dateByAddingTimeInterval:seconds];发布于 2009-10-21 23:26:24
nall让我找到了正确的方向。我在NSDate上拼凑了一个类别,它负责细节,既对10.5/10.6友好,又不使用碳。谢谢你的帮助,nall!
NSDate-Additions.h
#import <Foundation/Foundation.h>
@interface NSDate (NSDate_Additions)
+(NSTimeInterval) timeIntervalSinceSystemStartup;
-(NSTimeInterval) timeIntervalSinceSystemStartup;
+(NSDate *) dateOfSystemStartup;
+(NSDate *) dateWithCGEventTimestamp:(CGEventTimestamp)cgTimestamp;
@endNSDate-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发布于 2009-12-08 17:26:48
关于上面的代码NSDate-Additions.m。行"timeNano = time * sTimebaseInfo.numer / sTimebaseInfo.denom;“将导致iPhone OS 2.2.1上的内存损坏。修复方法是使用如下类型的强制转换: timeNano = time * (uint64_t)sTimebaseInfo.numer / sTimebaseInfo.denom;
https://stackoverflow.com/questions/1597383
复制相似问题