CMLogItem timestamp: Why so complicated?
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.
iPhone - core motion timestamp
Seems to be fine the way you do it. But why do you create a new instance of CMMotionManager? I use:
if ([motionManager isDeviceMotionAvailable] && [motionManager isDeviceMotionActive]) {
[motionManager stopDeviceMotionUpdates];
}
and to continue:
[motionManager startDeviceMotionUpdatesToQueue:operationQueue withHandler:deviceMotionHandler];
I was curious to see if there is still something wrong in iOS with the timestamps and thus I just tried out your code or similar thing using a static. Everything works as expected. Maybe your timestampReference
is overwritten somewhere else?
How to get exact time since iOS device booted?
After investigation of the data you provide. It is possible to conclude that CMLogItem
:
The CMLogItem class is a base class for Core Motion classes that
handle specific types of motion events. Objects of this class
represent a piece of time-tagged data that can be logged to a file.
CMLogItem defines only a read-only timestamp property that records the time a motion-event measurement was taken. Not the exact time since device was booted.
By ProcessInfo.processInfo.systemUptime
you going to get time from the last time it was restarted.
I believe data that you would like to obtain is quite specific and is under Private API of the iOS.
Nslog timestamp
Edit: Please see Nicolas Lauquin's answer. Per the comments, the following solution is not correct but is retained here for history (and because I can't delete it since it is currently marked accepted).
The timestamp
property is an NSTimeInterval
, so you should be able to do:
NSLog(@"Motion at time: %@",
[NSDate dateWithTimeIntervalSinceReferenceDate:devicemotion.timestamp]);
NSTimeInterval
is just a typedef'd double
type, so you could use %f
instead of %@
and log it directly.
Also, the docs don't indicate whether this timestamp is set against Apple's reference date or the standard *nix date, so you may need to use [NSDate dateWithTimeIntervalSince1970:]
if the aforementioned method returns dates far in the future.
As @davidbitton has suggested the CMDeviceMotion
's timestamp
is relative to the last device boot, the correct NSDate
could be derived by
NSDate *startupTime = [NSDate dateWithTimeIntervalSinceNow:
-1 * [[NSProcessInfo processInfo] systemUptime]];
NSDate *deviceMotionDate = [NSDate dateWithTimeInterval:devicemotion.timestamp
sinceDate:startupTime];
This should yield a roughly accurate NSDate
object, assuming @davidbitton is correct. (reference: NSProcessInfo -systemUptime)
However, given how complicated this is, I would now suggest for simplicity that, given the nature of the timestamp
property, that you log it in a format string as something like
"... event logged at %0.2f seconds since startup...", devicemotion.timestamp
In iOS, using Swift, how do you get the time of iPhone boot?
From the comments, I was able to find ProcessInfo.processInfo.systemUptime
which is the time since last boot. From this, and Date()
, I can get the time of start via:
time_of_last_boot = Date() - ProcessInfo.processInfo.systemUptime
Related Topics
How to Know If a Swiftui Button Is Enabled/Disabled
Flipping The Positions of Items in a UIstackview
Count Number of Instances of a Class Swift
Url(String:) Cannot Call Value of Non-Function Type 'string'
iOS Swift, Aws Cognito User Pool, Unable to Refresh Access Token
Struct Memberwise Initialization - Omitting Values for Properties That Have Defaults
Swift Version Build Configuration
Testing a Timer in Xcode with Xctest
Xcode 13.1 Bug Editing Dependency as Local
Detect What Is The Location of The Tap/Press Inside UIbutton Inside Sender Action Method
Swift Override Static Method Compile Error
Variable with Getter/Setter Cannot Have Initial Value, on Overridden Stored Property
Conversion Between Cgfloat and Nsnumber Without Unnecessary Promotion to Double
How to Map Over The Values of Zip2
Convert/Wrap Swift Struct as Nsvalue for Caanimation Purposes