本文介绍了CGEventTimestamp到NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在尝试将CGEventTimestamp(这是一个无符号的64位整数,大致表示自系统启动以来的纳秒),将其转换为NSDate.我知道有一种方法可以将自系统启动以来的时间"转换为NSTimeInterval或相对于参考日期的日期,但是找不到.

I'm simply trying to convert a CGEventTimestamp (which is an unsigned 64-bit integer roughly representing nanoseconds since system startup) into an NSDate. I know there's a way to convert "time since system startup" into either an NSTimeInterval or a date relative to a reference date, but I'm not finding it.

如何将CGEventTimestamp转换为NSDate将接受的内容?谢谢.

How do I convert a CGEventTimestamp into something NSDate will accept? Thanks.

(忘记提及,必须保持10.5友好,并尽可能避免使用Carbon)

(forgot to mention, needs to be 10.5 friendly and avoid Carbon if at all possible)

推荐答案

// 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];

这篇关于CGEventTimestamp到NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 13:06