Search code examples
iphonetimestampnslog

Nslog timestamp


I want to nslog the device motion timestamp property .

The device motion is in the class CMMotionManager.devicemotion.timestamp

Any ideas.


Solution

  • 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