Nslog时间戳 [英] Nslog timestamp
问题描述
我想对设备运动时间戳属性进行nslog记录.
I want to nslog the device motion timestamp property .
设备运动位于CMMotionManager.devicemotion.timestamp类中
The device motion is in the class CMMotionManager.devicemotion.timestamp
任何想法.
推荐答案
编辑:请参阅Nicolas Lauquin的回答.根据评论,以下解决方案不正确,但保留在此处以作历史记录(并且由于无法将其删除,因为它目前被标记为接受).
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).
timestamp
属性是一个 NSTimeInterval
,因此您应该可以:
The timestamp
property is an NSTimeInterval
, so you should be able to do:
NSLog(@"Motion at time: %@",
[NSDate dateWithTimeIntervalSinceReferenceDate:devicemotion.timestamp]);
NSTimeInterval
只是类型定义的 double
类型,因此您可以使用%f
而不是%@
并直接记录.
NSTimeInterval
is just a typedef'd double
type, so you could use %f
instead of %@
and log it directly.
此外,文档未指出此时间戳是针对Apple的参考日期还是标准的* nix日期设置的,因此,如果上述方法返回,则可能需要使用 [NSDate dateWithTimeIntervalSince1970:]
日期远在未来.
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.
由于@davidbitton建议 CMDeviceMotion
的 timestamp
与上次设备启动有关,因此正确的 NSDate
可以由
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];
假设@davidbitton是正确的,这应该产生一个大致准确的 NSDate
对象.(参考:
This should yield a roughly accurate NSDate
object, assuming @davidbitton is correct. (reference: NSProcessInfo -systemUptime)
但是,鉴于这有多么复杂,我现在为简单起见建议,考虑到 timestamp
属性的性质,您可以将其记录为格式字符串,例如
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
这篇关于Nslog时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!