3

I have extracted accerometer data from a android wearable. While looking at the data i realised the timestamp is not unix covertable. After research i saw the timestamp was actually nanoseconds in uptime. My question is the same as Accelerometer SensorEvent timestamp. However due to me not knowing Java i dont know how convert it using the solutions provided. Is there any python ways i can convert the nanoseconds in uptime into a readable date time format? An example of the timestamp would be "45900482044637".

Community
  • 1
  • 1
Spike7
  • 128
  • 1
  • 4
  • 15

3 Answers3

8

tl;dr

Duration.ofNanos ( 45_900_482_044_637L )

PT12H45M0.482044637S

java.time

Java 8 and later has a Duration class for this purpose, as part of the new java.time framework (see Tutorial). These new classes have nanosecond resolution (nine decimal places in fractional second). A Duration represents a span of time as a number of hours, minutes, and seconds.

Android currently does not use Java 8 technology, but there is a back-port of java.time to Java 6 & 7. Further adapted to Android in the ThreeTenABP project.

Note the L appended to numeric literal for a long. Also, underscores make lengthy numbers easier for humans to decipher.

long input = 45_900_482_044_637L;  

Let's convert that number to a Duration object.

Duration duration = Duration.ofNanos ( input );

When we generate a String representation of that Duration object, we get a String formatted using the ISO 8601 standard. That standard uses the pattern PnYnMnDTnHnMnS where the P marks the beginning and the T separates years-months-days from the hours-minutes-seconds.

    System.out.println ( "duration: " + duration );

The answer is twelve hours, forty-five minutes, and a fraction of a second.

duration: PT12H45M0.482044637S


Table of span-of-time classes in Java and in the ThreeTen-Extra project


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

Table of which java.time library to use with which version of Java or Android

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
2

To get the uptime in human-readable format in Python:

>>> from datetime import timedelta
>>> ns = 45900482044637
>>> print(timedelta(microseconds=round(ns, -3) // 1000))
12:45:00.482045
jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

Use:

long millis = TimeUnit.MILLISECONDS.convert(nanosecond, TimeUnit.NANOSECONDS);
Date date = new Date(millis );
DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd hh:mm aaa");
String dateFormatted = formatter.format(date);
mdtuyen
  • 4,470
  • 5
  • 28
  • 50