8

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

Pablo A. Martínez
  • 2,547
  • 2
  • 23
  • 28
  • See here: http://stackoverflow.com/questions/3498006/sensorevent-timestamp-to-absolute-utc-timestamp – goto10 Oct 14 '11 at 21:53

2 Answers2

4

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.

Community
  • 1
  • 1
manu3d
  • 991
  • 1
  • 8
  • 23
  • 1
    Thanks goto10 and manu3d, finally the answer is that the timestamp time is the device's uptime in nanosecs. – Pablo A. Martínez Oct 16 '11 at 18:51
  • 3
    It's not always. On my Nexus 4, it's nanoseconds since the epoch (start of 1970). Perhaps it could be inferred by assuming that the SensorEvent is going to be processed quickly and testing it against uptime and start of 1970. – rbncrthms Jun 23 '13 at 23:12
1

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.

  • SystemClock.elapsedRealtimeNanos() is the API you are looking for, sensorTimeStamp in device's local time reference = System.currentTimeMillis() + ((event.timestamp-SystemClock.elapsedRealtimeNanos())/1000000L); – Pablo A. Martínez Feb 19 '16 at 15:36
  • Did you find that mUptimeMillis change over time (in a single boot)? Which function did you use for atComponentsStartUp? – Dzung Nguyen Jul 15 '17 at 03:36