ios – NSTimeInterval到unix时间戳

我从cmmotionmanager获取CMDeviceMotion对象. CMDeviceMotion的一个属性是时间戳,表示为NSTimeInterval(double).根据文档,这允许“亚毫秒”时间戳精度.
[motionManager startDeviceMotionUpdatesToQueue:motionQueue withHandler:^(CMDeviceMotion *motion,NSError *error) { 
  NSLog(@"Sample: %d Timestamp: %f ",counter,motion.timestamp);
}

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

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

谢谢!

解决方法

在将磁力计值与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;

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...