Skip to content

Commit

Permalink
Improve Precision to 10 microseconds in timespan (dotnet/coreclr#24279)
Browse files Browse the repository at this point in the history
* Increasing precision properly and leaving the rounding of to casting operator

* disabling corefx tests in coreclr and addressing feedback

* correcting test project name

* Fix json

Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
  • Loading branch information
Anipik authored and dotnet-bot committed Apr 29, 2019
1 parent dc683e5 commit db218b4
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions netcore/System.Private.CoreLib/shared/System/TimeSpan.cs
Expand Up @@ -196,7 +196,7 @@ public int CompareTo(TimeSpan value)

public static TimeSpan FromDays(double value)
{
return Interval(value, MillisPerDay);
return Interval(value, TicksPerDay);
}

public TimeSpan Duration()
Expand Down Expand Up @@ -232,28 +232,27 @@ public override int GetHashCode()

public static TimeSpan FromHours(double value)
{
return Interval(value, MillisPerHour);
return Interval(value, TicksPerHour);
}

private static TimeSpan Interval(double value, int scale)
private static TimeSpan Interval(double value, double scale)
{
if (double.IsNaN(value))
throw new ArgumentException(SR.Arg_CannotBeNaN);
double tmp = value * scale;
double millis = tmp + (value >= 0 ? 0.5 : -0.5);
if ((millis > long.MaxValue / TicksPerMillisecond) || (millis < long.MinValue / TicksPerMillisecond))
double millis = value * scale;
if ((millis > long.MaxValue) || (millis < long.MinValue))
throw new OverflowException(SR.Overflow_TimeSpanTooLong);
return new TimeSpan((long)millis * TicksPerMillisecond);
return new TimeSpan((long)millis);
}

public static TimeSpan FromMilliseconds(double value)
{
return Interval(value, 1);
return Interval(value, TicksPerMillisecond);
}

public static TimeSpan FromMinutes(double value)
{
return Interval(value, MillisPerMinute);
return Interval(value, TicksPerMinute);
}

public TimeSpan Negate()
Expand All @@ -265,7 +264,7 @@ public TimeSpan Negate()

public static TimeSpan FromSeconds(double value)
{
return Interval(value, MillisPerSecond);
return Interval(value, TicksPerSecond);
}

public TimeSpan Subtract(TimeSpan ts)
Expand Down

0 comments on commit db218b4

Please sign in to comment.