开发者

UIEvent has timestamp. How can I generate an equivalent value on my own?

开发者 https://www.devze.com 2022-12-22 19:52 出处:网络
According to the cocoa documentation, timestamp on UIEvent is \"the number of seconds since system startup.\" It\'s an NSTimeInterval.

According to the cocoa documentation, timestamp on UIEvent is "the number of seconds since system startup." It's an NSTimeInterval.

I'd like to generate, as efficiently as po开发者_开发百科ssible, an equivalent number. Of course, I want to do this where UIEvent don't shine. :-)


Okay, I did a little digging and here is what I came up with:

#import <mach/mach.h>
#import <mach/mach_time.h>

+ (NSTimeInterval)timestamp
{
    // get the timebase info -- different on phone and OSX
    mach_timebase_info_data_t info;
    mach_timebase_info(&info);

    // get the time
    uint64_t absTime = mach_absolute_time();

    // apply the timebase info
    absTime *= info.numer;
    absTime /= info.denom;

    // convert nanoseconds into seconds and return
    return (NSTimeInterval) ((double) absTime / 1000000000.0);
}

This appears to be equivalent to timestamp from UIEvent, which given what mach_absolute_time() does makes a lot of sense.


Perhaps you could combine the NSDate method -timeIntervalSinceDate: and the mach framework-based function GetPIDTimeInNanoseconds to get to the same result.

0

精彩评论

暂无评论...
验证码 换一张
取 消