Xenorosth Xenorosth - 4 months ago 22
Java Question

Webkit/chrome timestamps to Human-Readable time in java

Question is in the title. I'm working a java program that will turn the entirety of the cookie's file in chrome, and turning it into text. However, I need the time stamps to be in something that is recognizable to humans.

What is the algorithm for converting the webkit time to human-readable time, or an example of a java program that can do it?

Answer

The time stamp format used in web browsers such as WebKik and Chrome is a 64-bit value for microseconds since Jan 1, 1601 00:00 UTC.

The algorithm to covert the WebKit/Chrome time would be:

  1. Calculate number of microseconds between the WebKit/Chrome time stamp and number of microseconds differential between Jan 1, 1970 00:00 UTC and Jan 1, 1601 00:00 UTC
  2. Convert the microseconds into a date time

Following is an example:

public LocalDateTime localDateTimeFromWebKitTimestamp(long webKitTimestamp) {
    long epochStart = LocalDateTime
            .from(LocalDateTime.of(1601, Month.JANUARY, 1, 0, 0))
            .until(LocalDateTime.of(1970, Month.JANUARY, 1, 0, 0), ChronoUnit.MICROS);
    long delta = webKitTimestamp - epochStart;
    return LocalDateTime.ofInstant(
            Instant.ofEpochMilli(TimeUnit.MICROSECONDS.toMillis(delta)), ZoneId.systemDefault());
}