Question

I'm reading timestamp values from SensorEvent data but I can't work out the reference time for these values. Android documentation just says "The time in nanosecond at which the event happened" As an example:

My current Android device date, October 14th 2011 23:29:56.421 (GMT+2)

System.currentTimeMillis * 1000000 (nanosec) = 1318627796431000000 (that's ok)

sensorevent.timestamp (nanosec) = 67578436328000 = 19 hours 46 min ????

May you help me?

thanks

Was it helpful?

Solution

It appears that what you are dealing with is the number of nanoseconds since the operating system started, also known as "uptime".

Further info on the issue: http://code.google.com/p/android/issues/detail?id=7981

I should add that the linked question SensorEvent.timestamp to absolute (utc) timestamp? deals with the same issue and is where I found the answer.

OTHER TIPS

I know that it's a very old question, but, I'm also struggling for converting SensorEvent.timestamp to a human readable time. So I'm writing here what I've understood so far and how I'm converting it in order to get better solutions from you guys. Any comments will be welcomed.

As I understood, SensorEvent.timestamp is an elapsed time since the device's boot-up. So I have to know the uptime of the device. So if there is an API returning device's boot-up, it will be very easy, but, I haven't found it. So I'm using SystemClock.elapsedRealtime() and System.currentTimeMillis() to 'estimate' a device's uptime. This is my code.

private long mUptimeMillis; // member variable of the activity or service
...
atComponentsStartUp...() {
    ...
    /* Call elapsedRealtime() and currentTimeMillis() in a row 
       in order to minimize the time gap */
    long elapsedRealtime = SystemClock.elapsedRealtime();
    long currentTimeMillis = System.currentTimeMillis();

    /* Get an uptime. It assume that elapsedRealtime() and
       currentTimeMillis() are called at the exact same time. 
       Actually they don't, but, ignore the gap 
       because it is not a significant value.
       (On my device, it's less than 1 ms)   */
    mUptimeMillis = (currentTimeMillis - elapsedRealtime); 
    ....
}
...
public void onSensorChanged(SensorEvent event) {
    ...
    eventTimeMillis = ((event.timestamp / 1000000) + mUptimeMillis);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(eventTimeMillis);
    ...
}

I think this works for Apps that a millisecond time error is okey. Please, leave your ideas.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top