gpt4 book ai didi

objective-c - CGEventTimestamp 到 NSDate

转载 作者:行者123 更新时间:2023-12-03 16:51:02 24 4
gpt4 key购买 nike

我只是想将 CGEventTimestamp(这是一个无符号 64 位整数,大致表示自系统启动以来的纳秒)转换为 NSDate。我知道有一种方法可以将“自系统启动以来的时间”转换为 NSTimeInterval 或相对于引用日期的日期,但我没有找到它。

如何将 CGEventTimestamp 转换为 NSDate 可以接受的内容?谢谢。

(忘了提及,需要对 10.5 友好,并尽可能避免使用 Carbon)

最佳答案

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

关于objective-c - CGEventTimestamp 到 NSDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1597383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com