0

I am trying to convert the timestamp captured from adb shell getevent by using the following command

adb shell getevent -lt /dev/input/event2 >filepath/filename.txt

This gives the output like the below

  [   19393.303318] EV_ABS       ABS_MT_TRACKING_ID   00000b87
  [   19393.303318] EV_ABS       ABS_MT_POSITION_X    00000180
  [   19393.303318] EV_ABS       ABS_MT_POSITION_Y    000004e2
  [   19393.303318] EV_ABS       ABS_MT_PRESSURE      0000004c
  [   19393.303318] EV_ABS       ABS_MT_TOUCH_MAJOR   00000001
  [   19393.303318] EV_SYN       SYN_REPORT           00000000

here the timestamp seen is 19393.303318 which is not in normal time format

How to convert it in to hh:mm:ss:SSS format using JAVA

If possible is there any other way to get proper time format for adb shell getevent

Thanks in advance

Siva
  • 1,078
  • 4
  • 18
  • 36
  • @AlexP. is there a way to convert this to time in Java There is a solution in C which converts timestamp to readable format http://stackoverflow.com/questions/2408976/struct-timeval-to-printable-format – Siva Dec 20 '15 at 23:51

3 Answers3

1

I finally found out that the output of getevent is based on the uptimeMillis() instead of the elapsedRealtime(). Just like the getEventTime: https://developer.android.com/reference/android/view/InputEvent.html#getEventTime().

I use this uptimeMillis get the correct timestamp.

Maciej Treder
  • 11,866
  • 5
  • 51
  • 74
xczhang
  • 166
  • 1
  • 3
-1

It looks like number of seconds since boot. To get absolute timestamp of moment of boot you can use something like this: System.currentTimeMillis() - SystemClock.elapsedRealtime().

koral
  • 2,513
  • 1
  • 32
  • 41
  • I want to convert timestamp to HH:mm:ss format – Siva Dec 20 '15 at 23:49
  • Assuming that you have value extracted from output as a `long` (you can use eg. `Scanner` for that). Then you can add it to boot time mentioned above and pass such summed timestamp to `new SimpleDateFormat("hh:mm:ss").format(new Date(timestamp))` – koral Dec 20 '15 at 23:53
  • is SystemClock.elapsedRealtime() is a android method ? – Siva Dec 21 '15 at 00:43
  • Yes: https://developer.android.com/reference/android/os/SystemClock.html#elapsedRealtime() – koral Dec 21 '15 at 00:45
  • No. I want a method in java – Siva Dec 21 '15 at 00:47
  • Is there any other way of capturing adb shell getevent along with the time ?? – Siva Dec 21 '15 at 00:48
  • You can get time since boot also by adb: `adb shell uptime`. – koral Dec 21 '15 at 00:50
-1

Here is your answer. I think you have to start googling for your queries. I am not good in java, But this took 2 hours for me for search and write this.Not properly arranged anyway this works and now I can use same to convert dmesg to adb shell date format. Note:- while replace date and uptime use - date;cat /proc/uptime inside adb shell to get both at same time(not fully accurate )

main method to convert getevent file

public static void convertGetEventFiletoHumanReadableTime(
        String geteventFileLocation) throws ParseException, IOException {

    SimpleDateFormat adbDatefmt = new SimpleDateFormat(
            "EEE MMM dd hh:mm:ss z yyyy");
    String adbDate = "Mon Jan  4 21:23:19 KST 2016"; //replace with your adb shell date
    String uptime = "33224.56 242604.45" //replace with your device cat adb shell /proc/uptime
    uptime = uptime.split(" ")[0];
    String event = "/dev/input/event1" //replace with event type you need to

    BufferedReader br = new BufferedReader(
            new FileReader(dmesgFileLocation));
    try {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        int lineNum = 0;
        while (line != null) {

            // System.out.println(lineNum + ": " + line); //for debug
            if (line.trim().length() > 0) {
                String eventTimeStamp = getEventTimeStamp(line, event);
                System.out.println("TIME " + eventTimeStamp);
                float eventTimeF = Float.parseFloat(eventTimeStamp);
                float eventTimeMillisecF = (float) (Math
                        .round(eventTimeF * 100.0) / 100.0);

                Date dmesghTime = convertGeteventTimestampToHumanReadable(
                        uptime, adbDate, eventTimeMillisecF);
                line = line.replace(eventTimeStamp,
                        adbDatefmt.format(dmesghTime));

                sb.append(line);
                sb.append(System.lineSeparator());
            }
            line = br.readLine();
            lineNum++;
        }
        String everything = sb.toString();
        // System.out.println(everything);
        try {
            FileWriter fw = new FileWriter("/getevent_humanTime.txt", true);
            fw.write(everything);
            fw.close();
        } catch (IOException e) {
            System.out.println("Something happened - here's what I know: ");
            e.printStackTrace();

        }
    } finally {
        br.close();
    }
}

method to get event timestamp from each line

public static String getEventTimeStamp(String event_string, String pattern) {

    Pattern pTime = Pattern
            .compile("\\[.*(\\d\\d\\d\\d\\d\\.\\d\\d\\d\\d\\d\\d)\\].*"
                    + pattern);
    Matcher mTime = pTime.matcher(event_string);

    String time = null;
    if (mTime.find()) {
        System.out.println("Time " + mTime.group(1));
        time = mTime.group(1);
    }
    return time;
}

method to convert epoch time to human readable [ round to millisec]

public static Date convertGeteventTimestampToHumanReadable(String  uptime,String adbDate , float secTimestamp)
        throws ParseException {


    System.out.println("DEVICE UPTIME IN SECS " + uptime);

    float uptimeMilliSecF = Float.parseFloat(uptime) * 1000;
    int uptimeMilliSec = Float.valueOf(uptimeMilliSecF).intValue();

    int dmesgtimeinMillisec = (int) (secTimestamp * 1000);

    SimpleDateFormat adbDatefmt = new SimpleDateFormat(
            "EEE MMM dd hh:mm:ss z yyyy");
    Date date = adbDatefmt.parse(adbDate);
    System.out.println("ADB DATE: " + date);

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);

    calendar.add(Calendar.MILLISECOND, -uptimeMilliSec);
    System.out.println("ADB DATE AT DEVICE UP: " + calendar.getTime());

    calendar.add(Calendar.MILLISECOND, dmesgtimeinMillisec);
    System.out.println("ADB DATE FOR DMESG EVENT: " + calendar.getTime());

    return calendar.getTime();

}
Rilwan
  • 2,251
  • 2
  • 19
  • 28
  • Thanks for posting the answer will see whether this works or not and will get back to you for any questions – Siva Jan 04 '16 at 13:06