問題描述
我從 CMMotionManager 獲取 CMDeviceMotion 對象.CMDeviceMotion 的屬性之一是時間戳,它表示為 NSTimeInterval (double).根據文檔,這允許亞毫秒"時間戳精度.
I'm getting CMDeviceMotion objects from CMMotionManager. One of the properties of the CMDeviceMotion is timestamp, which is expressed as a NSTimeInterval (double). This allows for "sub millisecond" timestamp precision, according to documentation.
[motionManager startDeviceMotionUpdatesToQueue:motionQueue withHandler:^(CMDeviceMotion *motion, NSError *error) {
NSLog(@"Sample: %d Timestamp: %f ",counter, motion.timestamp);
}
不幸的是,NSTimeInterval 是自上次設備啟動后計算的,這給以原始形式使用它帶來了重大挑戰.
Unfortunately, NSTimeInterval is calculated since last device boot, posing significant challenges to using it in its raw form.
有沒有人有工作代碼可以將此 NSTimeInterval 轉換為類似 Unix 的時間戳(UTC 時區)?
Does anyone have a working code to convert this NSTimeInterval into a Unix like timestamp (UTC timezone)?
謝謝!
推薦答案
在將磁力計值與 CoreMotion 事件進行比較時,我遇到了類似的問題.如果你想轉換這些 NSTimeIntervals 你只需要計算一次偏移量:
I had a similar problem when comparing magnetometer values with CoreMotion events. If you want to transform these NSTimeIntervals you just need to calculate the offset once:
// 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;
這篇關于NSTimeInterval 到 unix 時間戳的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!