/** * Converts uptime nanos to a real UTC timestamp in seconds. * * @param nanos * @return */ public static BigDecimal uptimeNanosToTimestamp(long nanos) { if (TimeUtil.secondsOffset == null) { calibrateNanosConversion(); } else { long currentMillis1 = System.currentTimeMillis(); long currentNanos = System.nanoTime(); long currentMillis2 = System.currentTimeMillis(); BigDecimal currentTimeStamp = DecimalTimeUnit.MILLISECONDS.toSeconds((currentMillis1 + currentMillis2) / 2.0); if (TimeUtil._uptimeNanosToTimestamp(currentNanos) .subtract(currentTimeStamp) .abs() .doubleValue() > TimeUtil.CLOCK_OFFSET_TOLERANCE) { calibrateNanosConversion(); } } return TimeUtil._uptimeNanosToTimestamp(nanos); }
/** * Returns a BigDecimal timestamp in seconds with microsecond precision, using System.nanoTime() * and uptimeNanosToTimestamp() * * @return */ public static BigDecimal getTimestampWithMicroPrecision() { return TimeUtil.uptimeNanosToTimestamp(System.nanoTime()); }