package { import flash.utils.getTimer; /** * TimeSync maintains a seperate time value from the system clock. */ public class TimeSync { private static var _instance:TimeSync; public static function getInstance():TimeSync { if (_instance == null) _instance = new TimeSync(); return _instance; } /** * Internal time-stamp value to sync to. * Must be set using sync(). */ protected var _syncStamp:Number; /** * Time-stamp for the internal flash getTimer(). */ protected var _timerStamp:Number; public function TimeSync() {} /** * The time elapsed since the last sync, in millseconds. */ public function get delta():Number { return (getTimer() - _timerStamp); } /** * The synchronized time value, in milliseconds. Pass this into * a new Date object to get formatted time: new Date(time); */ public function get time():Number { return (_syncStamp + delta); } /** * Sync the timer to a new time value. * @param time Unix time value in milliseconds */ public function sync(time:Number):void { _syncStamp = time; _timerStamp = getTimer(); } } }