diff --git a/projects/samskivert/src/java/com/samskivert/util/RunAnywhere.java b/projects/samskivert/src/java/com/samskivert/util/RunAnywhere.java index 5674408e..53323a8f 100644 --- a/projects/samskivert/src/java/com/samskivert/util/RunAnywhere.java +++ b/projects/samskivert/src/java/com/samskivert/util/RunAnywhere.java @@ -1,10 +1,12 @@ // -// $Id: RunAnywhere.java,v 1.4 2003/07/12 02:46:03 ray Exp $ +// $Id: RunAnywhere.java,v 1.5 2003/07/29 00:43:54 mdb Exp $ package com.samskivert.util; import java.awt.event.InputEvent; +import com.samskivert.Log; + /** * Write once, run anywhere. Well, at least that's what it * said in the brochures. For those less than fresh days, you might need @@ -39,6 +41,32 @@ public class RunAnywhere return _isLinux; } + /** + * Returns {@link System#currentTimeMillis}, but works around a bug on + * WinXP that causes time to sometimes leap into the past. + */ + public static final long currentTimeMillis () + { + long stamp = System.currentTimeMillis(); + + // on WinXP the time sometimes seems to leap into the past; here + // we do what we can to work around this insanity by simply + // stopping time rather than allowing it to go into the past + if (stamp < _lastStamp) { + // only warn once per time anomaly + if (stamp > _lastWarning) { + Log.warning("Someone call Einstein! The clock is " + + "running backwards [dt=" + + (stamp - _lastStamp) + "]."); + _lastWarning = _lastStamp; + } + stamp = _lastStamp; + } + _lastStamp = stamp; + + return stamp; + } + /** * Returns the timestamp associated with the supplied event except on * the Macintosh where it returns {@link System#currentTimeMillis} @@ -48,7 +76,7 @@ public class RunAnywhere */ public static long getWhen (InputEvent event) { - return (isWindows() || isMacOS()) ? System.currentTimeMillis() + return (isWindows() || isMacOS()) ? currentTimeMillis() : event.getWhen(); } @@ -64,6 +92,9 @@ public class RunAnywhere * is first loaded. */ protected static boolean _isLinux; + /** Used to ensure that the timer is sane. */ + protected static long _lastStamp, _lastWarning; + static { try { String osname = System.getProperty("os.name");