什么是基于iPhone的mach_absolute_time [英] What is mach_absolute_time based on on iPhone

查看:393
本文介绍了什么是基于iPhone的mach_absolute_time的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码来跟踪上次重启:

I use this code to keep track of last reboot:

+ (float) secondsSinceLastReboot{
     return ((float)(mach_absolute_time())) * ((float)timebase.numer) / ((float)timebase.denom) / 1000000000.0f;
}

我假设mach_absolute_time()基于上次设备启动时间,就像它打开一样一个mac。它似乎不是基于此。我实际上根本不知道它是基于什么。

I assumed mach_absolute_time() was based on last device boot time like it is on a mac. It doesn't seem to be based on that. I actually have no idea what it is based on.

看看以下行为(今天的日期是2009-09-20):

Look at the following behaviour (today's date is 2009-09-20):

lastRebootTime = [[NSDate date] addTimeInterval:-[self secondsSinceLastReboot]];
//last Reboot Time will contain : 2009-09-20 07:42:14 +0100

我绝对肯定我当时没有重启设备。我的设备一周内没有启动。

I'm absolutely certain I did not reboot my device at that time. My device hasn't been booted in a week.

此外,当我从电缆上取下设备并运行此应用程序时,似乎当设备进入休眠状态时,lastRebootTime将来会开始转移。似乎mach_absolute_time没有考虑睡眠时间。或者我错了吗?

Furthermore, when I unhook my device from the cable and run this app , it seems that when the device goes to sleep, the lastRebootTime starts shifting in the future. It seems mach_absolute_time doesn't keep account for sleep time. Or am i wrong about this?

我真的希望能够从设备上次重启时获得时间戳。有什么想法吗?

I would really like to be able to get a timestamp from when the device last rebooted. Any idea's?

推荐答案

我自己也遇到了一些麻烦。没有很多好的文档,所以我选择了实验。这是我能够确定的:

Had some trouble with this myself. There isn't a lot of good documentation, so I went with experimentation. Here's what I was able to determine:

mach_absolute_time取决于设备的处理器。它返回自设备上次重启以来的刻度(也称为正常运行时间)。为了以人类可读的形式获取它,您必须通过mach_timebase_info(比率)的结果对其进行修改,该结果将返回十亿分之一秒(或纳秒)。为了使它更有用,我使用如下所示的函数:

mach_absolute_time depends on the processor of the device. It returns ticks since the device was last rebooted (otherwise known as uptime). In order to get it in a human readable form, you have to modify it by the result from mach_timebase_info (a ratio), which will return billionth of seconds (or nanoseconds). To make this more usable I use a function like the one below:

#include <mach/mach_time.h>

int getUptimeInMilliseconds()
{
    const int64_t kOneMillion = 1000 * 1000;
    static mach_timebase_info_data_t s_timebase_info;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        (void) mach_timebase_info(&s_timebase_info);
    });

    // mach_absolute_time() returns billionth of seconds,
    // so divide by one million to get milliseconds
    return (int)((mach_absolute_time() * s_timebase_info.numer) / (kOneMillion * s_timebase_info.denom));
}

这篇关于什么是基于iPhone的mach_absolute_time的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆