millisecond & nanosecond precision replacement for system.currentTimeMillis()

Tired of not being able to get the time with a millisecond precision on windows?
Here is a ‘fix’. Works on 1.5+ only.
That was submitted (in a somewhat different and reduced version) as a RFE to Sun, today. /me crosses fingers…

note: also includes a nanosecond precise date, but which is not -of course- compatible with Date class and others.

/**
 * DOCUMENT ME!
 *
 * @author pepe
 */
public class PreciseInternalDate
{
    //~ Static fields/initializers *********************************************
    /** DOCUMENT ME! */
    static long lastDate;

    /** DOCUMENT ME! */
    static long lastDateNanos;

    /** DOCUMENT ME! */
    static long nanos;

    static
    {
        lastDate          = System.currentTimeMillis(  );
        nanos             = System.nanoTime(  );
        lastDateNanos     = lastDate * 1000000;
    }

    //~ Constructors ***********************************************************
    /**
     * Creates a new instance of PreciseInternalDate
     */
    private PreciseInternalDate(  ) {}

    //~ Methods ****************************************************************
    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    static public long currentTimeMillis(  )
    {
        checkForClockReset(  );

        return lastDate + ( ( System.nanoTime(  ) - nanos ) / 1000000 );
    }


    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    static public long currentTimeNanos(  )
    {
        checkForClockReset(  );

        return lastDateNanos + ( System.nanoTime(  ) - nanos );
    }


    /**
     * DOCUMENT ME!
     */
    static private void checkForClockReset(  )
    {
        long newCurTime = System.currentTimeMillis(  );
        long predicted = lastDate + ( ( System.nanoTime(  ) - nanos ) / 1000000 );
        long delta = predicted - newCurTime;

        if ( ( delta > 200 ) || ( delta < 0 ) ) // let's say that if there is a decal of over 200ms, the clock got reset and we need to change the date accordingly (could be the user changing date on his machine, or through nntp server)
        { // also, delta should only be positive, because predicted is of better precision than newCurtime. Otherwise, it might also be a clock reset.
            lastDate          = newCurTime;
            lastDateNanos     = newCurTime * 1000000;
        }
    }
}

Disclaimer: this code is not intended to be ran in a nuclear facility, nor it should run in a machine with an uptime greater than 292 years. :wink:

jalopy rocks.

Arf… Yes, pretty much. But my lazyness breaks it down. :confused:

My rfe got closed… “not a bug”… well, yes, of course… :s
At least it got reviewed and there were some meaningful comments…I’m just a bit decieved they didn’t scratch the surface more than that. Bah, i just asked for it, the class was not complete…

Just in case someone is interested, i did open a java.net project with a much better (documented, clean, enhanced) version of that code.
https://preciseinternaldate.dev.java.net/