6

我 从CMMotionManager获取CMDeviceMotion对象。CMDeviceMotion 的属性之一是时间戳,它表示为 NSTimeInterval (double)。根据文档,这允许“亚毫秒”时间戳精度。

[motionManager startDeviceMotionUpdatesToQueue:motionQueue withHandler:^(CMDeviceMotion *motion, NSError *error) { 
  NSLog(@"Sample: %d Timestamp: %f ",counter,  motion.timestamp);
}

不幸的是,NSTimeInterval 是自上次设备启动后计算的,这对以原始形式使用它构成了重大挑战。

有没有人有工作代码可以将此 NSTimeInterval 转换为类似 Unix 的时间戳(UTC 时区)?

谢谢!

4

2 回答 2

15

在将磁力计值与 CoreMotion 事件进行比较时,我遇到了类似的问题。如果你想转换这些 NSTimeIntervals 你只需要计算一次偏移量:

// during initialisation

// Get NSTimeInterval of uptime i.e. the delta: now - bootTime
NSTimeInterval uptime = [NSProcessInfo processInfo].systemUptime;

// Now since 1970
NSTimeInterval nowTimeIntervalSince1970 = [[NSDate date] timeIntervalSince1970];

// Voila our offset
self.offset = nowTimeIntervalSince1970 - uptime;
于 2011-09-28T12:13:44.350 回答
2

我想它应该被用作一种相对措施,以允许您正确地对运动事件进行排序,因此它们尽可能地实现了轻量级。

我想不出你为什么需要运动事件的实际日期和时间,因为它们几乎都是立即处理的。但是,如果您真的想要,您必须获取一个事件的时间戳,将其与当前日期一起使用来计算基准日期,然后使用您的基准日期来推导出后续事件的实际时间。

于 2011-09-27T22:18:36.373 回答