NSTimeInterval到unix时间戳

Ale*_*one 6 timestamp nstimeinterval ios core-motion

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

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

不幸的是,NSTimeInterval是自上次设备启动以来计算的,对以原始形式使用它提出了重大挑战.

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

谢谢!

Kay*_*Kay 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;
Run Code Online (Sandbox Code Playgroud)