2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.bgpcep.programming;
10 import java.math.BigInteger;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.Nanotime;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
17 * Util methods for {@link Nanotime}.
19 public final class NanotimeUtil {
20 private static final Logger LOG = LoggerFactory.getLogger(NanotimeUtil.class);
21 private static final BigInteger MILLION = BigInteger.valueOf(1000000);
22 private static volatile BigInteger nanoTimeOffset = null;
24 private NanotimeUtil() {
28 * Returns current time in nanoseconds.
30 * @return Nanotime object filled with current time in nanoseconds.
32 public static Nanotime currentTime() {
33 return new Nanotime(BigInteger.valueOf(System.currentTimeMillis()).multiply(MILLION));
37 * Returns calibrated current JVM nano time.
39 * @return Nanotime object filled with current JVM nano time.
41 public static Nanotime currentNanoTime() {
42 if (nanoTimeOffset == null) {
45 return new Nanotime(BigInteger.valueOf(System.nanoTime()).add(nanoTimeOffset));
49 * Calibrates the offset between the real-time clock providing System.currentTimeMillis() and the monotonic clock
50 * providing System.nanoTime(). This method should be called whenever there is a hint of the two diverging: either
51 * when time shifts or periodically.
53 public static void calibrate() {
54 final long tm1 = System.currentTimeMillis();
55 final long nt1 = System.nanoTime();
56 final long tm2 = System.currentTimeMillis();
57 final long nt2 = System.nanoTime();
59 LOG.debug("Calibrated currentTime and nanoTime to {}m <= {}n <= {}m <= {}n", tm1, nt1, tm2, nt2);
61 final BigInteger tm = BigInteger.valueOf(tm1).add(BigInteger.valueOf(tm2)).divide(BigInteger.valueOf(2));
62 final BigInteger nt = BigInteger.valueOf(nt1).add(BigInteger.valueOf(nt2)).divide(BigInteger.valueOf(2));
64 nanoTimeOffset = tm.multiply(MILLION).subtract(nt);