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
This commit is contained in:
Markus Ewald 2009-06-24 20:24:21 +00:00
parent 839d46ecf1
commit 5d36825fc8

View File

@ -149,34 +149,32 @@ namespace Nuclex.Support.Scheduling {
private void checkForTimeAdjustment() { private void checkForTimeAdjustment() {
// Grab the current date/time and timer ticks in one go // Grab the current date/time and timer ticks in one go
DateTime currentLocalTime = DateTime.Now; long currentDateTimeTicks = DateTime.UtcNow.Ticks;
long currentTicks = Ticks; long currentStopwatchTicks = Ticks;
// Calculate the number of timer ticks that have passed since the last check and // 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 // extrapolate the local date/time we should be expecting to see
long ticksSinceLastCheck = currentTicks - lastCheckedTicks; long ticksSinceLastCheck = currentStopwatchTicks - lastCheckedStopwatchTicks;
DateTime expectedLocalTime = new DateTime( long expectedLocalTimeTicks = this.lastCheckedDateTimeTicks + ticksSinceLastCheck;
lastCheckedLocalTime.Ticks + ticksSinceLastCheck, DateTimeKind.Local
);
// Find out by what amount the actual local date/time deviates from // Find out by what amount the actual local date/time deviates from
// the extrapolated date/time and trigger the date/time adjustment event if // the extrapolated date/time and trigger the date/time adjustment event if
// we can reasonably assume that the system date/time have been adjusted. // 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) { if(deviationTicks > TimeAdjustmentToleranceTicks) {
OnDateTimeAdjusted(this, EventArgs.Empty); OnDateTimeAdjusted(this, EventArgs.Empty);
} }
// Remember the current local date/time and timer ticks for the next run // Remember the current local date/time and timer ticks for the next run
this.lastCheckedLocalTime = currentLocalTime; this.lastCheckedDateTimeTicks = currentDateTimeTicks;
this.lastCheckedTicks = currentTicks; this.lastCheckedStopwatchTicks = currentStopwatchTicks;
} }
/// <summary>Last local time we checked for a date/time adjustment</summary> /// <summary>Last local time we checked for a date/time adjustment</summary>
private DateTime lastCheckedLocalTime; private long lastCheckedDateTimeTicks;
/// <summary>Timer ticks at which we last checked the local time</summary> /// <summary>Timer ticks at which we last checked the local time</summary>
private long lastCheckedTicks; private long lastCheckedStopwatchTicks;
/// <summary>Number of ticks per Stopwatch time unit</summary> /// <summary>Number of ticks per Stopwatch time unit</summary>
private static double tickFrequency; private static double tickFrequency;