Uptime iOS Objective-C - millisecond precision

懵懂的女人 提交于 2019-12-05 00:56:58

问题


I'm trying to get uptime for iOS. I was using mach_absolute_time - but I found that it paused during sleep.

I found this snippet:

- (time_t)uptime
{
    struct timeval boottime;
    int mib[2] = {CTL_KERN, KERN_BOOTTIME};
    size_t size = sizeof(boottime);
    time_t now;
    time_t uptime = -1;

    (void)time(&now);

    if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && boottime.tv_sec != 0)
    {
        uptime = now - boottime.tv_sec;
    }
    return uptime;
}

It does the trick. BUT, it's returning whole seconds. Any way to get milliseconds out of this?


回答1:


The kernel does not (apparently) store a higher-resolution timestamp of its boot time.

KERN_BOOTTIME is implemented by the sysctl_boottime function in bsd/kern/kern_sysctl.c. It calls boottime_sec.

boottime_sec is implemented in bsd/kern/kern_time.c. It calls clock_get_boottime_nanotime, which has a promising name.

clock_get_boottime_nanotime is implemented in osfmk/kern/clock.c. It is hard-coded to always return 0 in its nanosecs argument.




回答2:


If you want something pure Objective-C, try

NSTimeInterval uptime = [[NSProcessInfo processInfo] systemUptime];

(NSTimeInterval is a typedef for double, representing seconds.)




回答3:


I know it's probably too late, but there you go:

+ (NSTimeInterval)uptime {
    struct timeval boottime;
    int mib[2] = {CTL_KERN, KERN_BOOTTIME};
    size_t size = sizeof(boottime);

    struct timeval now;
    struct timezone tz;
    gettimeofday(&now, &tz);

    double uptime = -1;

    if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && boottime.tv_sec != 0) {
        uptime = now.tv_sec - boottime.tv_sec;
        uptime += (double)(now.tv_usec - boottime.tv_usec) / 1000000.0;
    }
    return uptime;
} 


来源:https://stackoverflow.com/questions/15995995/uptime-ios-objective-c-millisecond-precision

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!