内部时钟
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;
}
网友评论