Search code examples
javautcntp

NTP timestamp to UTC; 32 bit unsigned seconds + 32 bit fraction field


T1 = C50204ECEC42EE92

T2 = C50204EBD3E8DDA4

The timestamp format includes the first 32-bit unsigned seconds as a field spanning 136 years and the 32-bit fraction field resolving 232 picoseconds.

T1 can be resolved as Sep 27, 2004 03:18:04.922896299 UTC. How can I write a program to convert T2 or similar into UTC time.


Solution

  • public static ZonedDateTime parseNtp(String ts) {
        long seconds = Long.parseLong(ts.substring(0, 8), 16);
        long fraction = Long.parseLong(ts.substring(8), 16);
        return LocalDateTime.parse("1900-01-01T00:00:00").atZone(ZoneId.of("UTC"))
                .plusSeconds(seconds)
                .plusNanos((long)(1000000000.0 / (1L << 32) * fraction));
    }
    

    Ideone Demo