移动设备运行时长

2023-03-09  本文已影响0人  大成小栈

内部时钟
https://www.cnblogs.com/ysw24/p/4846575.html

设备运行时长,也称为开机时长,常用于时间校准。

// 获取当前设备运行时长,不受系统时间影响
- (NSTimeInterval)getUptime {
    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;
}
上一篇 下一篇

猜你喜欢

热点阅读