Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added Math.Max(1, *) to preven the devide by zero exception in issue #48

  • Loading branch information...
commit efd5d8c41cd2ce4e38eca8b5451ceb198ac3a29e 1 parent 27b16f3
@nberardi nberardi authored
Showing with 5 additions and 10 deletions.
  1. +5 −10 src/System/DateTimePrecise.cs
View
15 src/System/DateTimePrecise.cs
@@ -72,24 +72,19 @@ public DateTime GetUtcNow()
if (stopWatchTicks < immutable.ObservedTicks + _synchronizePeriodStopwatchTicks)
{
return immutable.BaseTime.AddTicks(((
- stopWatchTicks - immutable.ObservedTicks) * ClockTickFrequency) / (
- immutable.StopWatchFrequency));
+ stopWatchTicks - immutable.ObservedTicks) * ClockTickFrequency) / (immutable.StopWatchFrequency));
}
else
{
var utc = DateTime.UtcNow;
- DateTime tBaseNew = immutable.BaseTime.AddTicks(((
- stopWatchTicks - immutable.ObservedTicks) * ClockTickFrequency) / (
- immutable.StopWatchFrequency));
+ DateTime tBaseNew = immutable.BaseTime.AddTicks(((stopWatchTicks - immutable.ObservedTicks) * ClockTickFrequency) / (immutable.StopWatchFrequency));
_immutable = new DateTimePreciseSafeImmutable(
utc,
tBaseNew,
stopWatchTicks,
- ((stopWatchTicks - immutable.ObservedTicks) * ClockTickFrequency * 2) /
- (utc.Ticks - immutable.ObservedTime.Ticks + utc.Ticks +
- utc.Ticks - tBaseNew.Ticks - immutable.ObservedTime.Ticks));
+ ((stopWatchTicks - immutable.ObservedTicks) * ClockTickFrequency * 2) / (3 * utc.Ticks - 2 * immutable.ObservedTime.Ticks - tBaseNew.Ticks));
return tBaseNew;
}
@@ -113,14 +108,14 @@ public DateTimeOffset GetNowOffset()
return new DateTimeOffset(GetNow());
}
- private sealed class DateTimePreciseSafeImmutable
+ private sealed struct DateTimePreciseSafeImmutable
{
public DateTimePreciseSafeImmutable(DateTime observedTime, DateTime baseTime, long observedTicks, long stopWatchFrequency)
{
ObservedTime = observedTime;
BaseTime = baseTime;
ObservedTicks = observedTicks;
- StopWatchFrequency = stopWatchFrequency;
+ StopWatchFrequency = Math.Max(1, stopWatchFrequency);
}
public readonly DateTime ObservedTime;
Please sign in to comment.
Something went wrong with that request. Please try again.