From 28bdcd942d0922930f279106ecd35b0fa0c3bb7b Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 29 Jul 2003 00:27:32 +0000 Subject: [PATCH] Added a workaround to the time fluctuation WinXP bug. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2735 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../media/timer/SystemMediaTimer.java | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/java/com/threerings/media/timer/SystemMediaTimer.java b/src/java/com/threerings/media/timer/SystemMediaTimer.java index a81e931f2..5f05cd068 100644 --- a/src/java/com/threerings/media/timer/SystemMediaTimer.java +++ b/src/java/com/threerings/media/timer/SystemMediaTimer.java @@ -1,8 +1,10 @@ // -// $Id: SystemMediaTimer.java,v 1.1 2002/11/20 02:17:38 mdb Exp $ +// $Id: SystemMediaTimer.java,v 1.2 2003/07/29 00:27:32 mdb Exp $ package com.threerings.media.timer; +import com.threerings.media.Log; + /** * Implements the {@link MediaTimer} interface using {@link * System#currentTimeMillis} to obtain timing information. @@ -23,7 +25,23 @@ public class SystemMediaTimer implements MediaTimer // documentation inherited from interface public long getElapsedMillis () { - return System.currentTimeMillis() - _resetStamp; + long stamp = System.currentTimeMillis() - _resetStamp; + + // on WinXP the time sometimes seems to leap into the + // past; here we do our best to work around this insanity + 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; } // documentation inherited from interface @@ -34,4 +52,7 @@ public class SystemMediaTimer implements MediaTimer /** The time at which this timer was last reset. */ protected long _resetStamp = System.currentTimeMillis(); + + /** Used to ensure that the timer is sane. */ + protected long _lastStamp, _lastWarning; }