Search code examples
iostimestampnsdatecore-motion

iOS Core Motion how to convert timestamp to Date?


I'm examining CMRecordedAccelerometerData and it has a timestamp, defined as:

The timestamp is the amount of time in seconds since the device booted.

How do I convert timestamp from device last boot to NSDate?

For example, the system provides a CMRecordedAccelerometerData object with a timestamp value of: 1030958.895134

If I use any of the available reference frames (1970, reference date), I will get a wrong date, not in 2019. I want the real date when the event was recorded.

enter image description here


Solution

  • This answer comes a bit late I guess, but, first, you can get the boot time by subtracting the uptime ProcessInfo.processInfo.systemUptime from now, but otherwise from iOS 9+ ProcessInfo.processInfo.systemUptime has the date property startDate, which should be what you were after in the first place.