All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.quasardb.qdb.ts.NanoClock Maven / Gradle / Ivy

Go to download

API for the JNI components of the QuasarDB API for Java. Should not be included directly.

There is a newer version: 3.14.1
Show newest version
package net.quasardb.qdb.ts;

import java.time.ZoneId;
import java.time.Instant;
import java.time.Clock;

/**
 * Nanosecond precision clock implementation. Used by {@link Timespec} as its
 * default Clock backend.
 *
 * @see Timespec#now()
 */
public class NanoClock extends Clock
{
    private final Clock clock;
    private final long initialNanos;
    private final Instant initialInstant;

    public NanoClock()
    {
        this(java.time.Clock.systemUTC());
    }

    public NanoClock(final Clock clock)
    {
        this.clock = clock;
        initialInstant = clock.instant();
        initialNanos = getSystemNanos();
    }

    @Override
    public ZoneId getZone()
    {
        return clock.getZone();
    }

    @Override
    public Instant instant()
    {
        return initialInstant.plusNanos(getSystemNanos() - initialNanos);
    }

    @Override
    public NanoClock withZone(final ZoneId zone)
    {
        return new NanoClock(clock.withZone(zone));
    }

    private long getSystemNanos()
    {
        return System.nanoTime();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy