Nslog时间戳

Abd*_*mad 2 iphone timestamp nslog

我想nslog设备运动时间戳属性.

设备动作位于CMMotionManager.devicemotion.timestamp类中

有任何想法吗.

Nic*_*uin 10

这里是我实施的解决方案,因为日期是根据Apple文档:

时间戳是自手机启动以来的秒数.

我首先将originDate保存在第一个mesure(如果我的NSDate为nil).

[self.motionManager startGyroUpdatesToQueue:self.queue withHandler:^(CMGyroData *gyroData, NSError *error) {
    if (self.originDate == nil) {
        self.originDate = [NSDate dateWithTimeIntervalSinceNow:-gyroData.timestamp];
    }   
}];
Run Code Online (Sandbox Code Playgroud)

然后我可以显示我想要这样的真实日期:

[NSDate dateWithTimeInterval:mygyroData.timestamp sinceDate:self.originDate]
Run Code Online (Sandbox Code Playgroud)

如果需要重新启动某些测量,请不要忘记将originDate重置为nil.


Ben*_*her 6

编辑:请参阅 Nicolas Lauquin 的回答。根据评论,以下解决方案不正确,但保留在此处作为历史记录(并且因为我无法删除它,因为它当前被标记为已接受)。


timestamp属性是 an NSTimeInterval,因此您应该能够执行以下操作:

NSLog(@"Motion at time: %@",
[NSDate dateWithTimeIntervalSinceReferenceDate:devicemotion.timestamp]);
Run Code Online (Sandbox Code Playgroud)

NSTimeInterval只是一个 typedefdouble类型,所以你可以使用%f而不是%@直接记录它。

此外,文档没有指出此时间戳是针对 Apple 的参考日期还是标准 *nix 日期设置的,因此[NSDate dateWithTimeIntervalSince1970:]如果上述方法返回未来很远的日期,您可能需要使用。

正如@davidbitton 所建议的CMDeviceMotion'stimestamp是相对于上次设备启动的,正确的NSDate可以通过

NSDate *startupTime = [NSDate dateWithTimeIntervalSinceNow:
                          -1 * [[NSProcessInfo processInfo] systemUptime]];

NSDate *deviceMotionDate = [NSDate dateWithTimeInterval:devicemotion.timestamp 
                                              sinceDate:startupTime];
Run Code Online (Sandbox Code Playgroud)

NSDate假设@davidbitton 是正确的,这应该会产生一个大致准确的对象。(参考:NSProcessInfo -systemUptime

但是,鉴于这有多复杂,为了简单起见,我现在建议,鉴于该属性的timestamp性质,您将其记录在格式字符串中,例如

"... event logged at %0.2f seconds since startup...", devicemotion.timestamp
Run Code Online (Sandbox Code Playgroud)

  • 这不起作用,因为设备运动样本的时间戳是设备上次启动时的 NSTimeInterval。它不是来自 2001 年 1 月 1 日或 1970 年的常见时代。 (5认同)