Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMLogItem timestamp: Why so complicated?

I receive a CMLogItem from a CoreMotion query in swift (could be accelerometer, gyroscope). Now, I want to get the timestamp of that sample, preferably as a Date() object. CMLogItems have a property .timestamp of type TimeInterval.

The documentation tells me the following:

The CMLogItem class defines a read-only timestamp property that records the time a motion-event measurement was taken.

However, I am not sure how to convert this timestamp to a Date() object because I dont know what the timestamp is referring to.

Another documentation says:

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

But this seems really weird and I dont understand why apple would create such an inconsistent and complicated API.

like image 461
DeveloBär Avatar asked Nov 11 '18 16:11

DeveloBär


Video Answer


1 Answers

Proper answer is:

extension CMLogItem {
    static let bootTime = Date(timeIntervalSinceNow: -ProcessInfo.processInfo.systemUptime)

    func startTime() -> Date {
        return CMLogItem.bootTime.addingTimeInterval(self.timestamp)
    }
}

This gives us stable, monotonic results, which is not a case, when bootTime is computed every time startTime is called.

like image 58
omikron Avatar answered Oct 05 '22 22:10

omikron