From 5d36825fc8cf14580512487866b58b0780744153 Mon Sep 17 00:00:00 2001 From: Markus Ewald Date: Wed, 24 Jun 2009 20:24:21 +0000 Subject: [PATCH] Attempt to fix a possible bug in the generic time source class which could potentially attempt to construct an instance of System.DateTime with an invalid tick count git-svn-id: file:///srv/devel/repo-conversion/nusu@155 d2e56fa2-650e-0410-a79f-9358c0239efd --- Source/Scheduling/GenericTimeSource.cs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/Source/Scheduling/GenericTimeSource.cs b/Source/Scheduling/GenericTimeSource.cs index db27ec8..a20e829 100644 --- a/Source/Scheduling/GenericTimeSource.cs +++ b/Source/Scheduling/GenericTimeSource.cs @@ -149,34 +149,32 @@ namespace Nuclex.Support.Scheduling { private void checkForTimeAdjustment() { // Grab the current date/time and timer ticks in one go - DateTime currentLocalTime = DateTime.Now; - long currentTicks = Ticks; + long currentDateTimeTicks = DateTime.UtcNow.Ticks; + long currentStopwatchTicks = Ticks; // Calculate the number of timer ticks that have passed since the last check and // extrapolate the local date/time we should be expecting to see - long ticksSinceLastCheck = currentTicks - lastCheckedTicks; - DateTime expectedLocalTime = new DateTime( - lastCheckedLocalTime.Ticks + ticksSinceLastCheck, DateTimeKind.Local - ); + long ticksSinceLastCheck = currentStopwatchTicks - lastCheckedStopwatchTicks; + long expectedLocalTimeTicks = this.lastCheckedDateTimeTicks + ticksSinceLastCheck; // Find out by what amount the actual local date/time deviates from // the extrapolated date/time and trigger the date/time adjustment event if // we can reasonably assume that the system date/time have been adjusted. - long deviationTicks = Math.Abs(expectedLocalTime.Ticks - currentLocalTime.Ticks); + long deviationTicks = Math.Abs(expectedLocalTimeTicks - currentDateTimeTicks); if(deviationTicks > TimeAdjustmentToleranceTicks) { OnDateTimeAdjusted(this, EventArgs.Empty); } // Remember the current local date/time and timer ticks for the next run - this.lastCheckedLocalTime = currentLocalTime; - this.lastCheckedTicks = currentTicks; + this.lastCheckedDateTimeTicks = currentDateTimeTicks; + this.lastCheckedStopwatchTicks = currentStopwatchTicks; } /// Last local time we checked for a date/time adjustment - private DateTime lastCheckedLocalTime; + private long lastCheckedDateTimeTicks; /// Timer ticks at which we last checked the local time - private long lastCheckedTicks; + private long lastCheckedStopwatchTicks; /// Number of ticks per Stopwatch time unit private static double tickFrequency;