Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 5fcd92c

Browse files
authored
Improve TimeSpan precision (#26992) (#27010)
Change multiplying by (x * (1.0 / BigValue)) on (x / BigValue). Fix #41380
1 parent 9783827 commit 5fcd92c

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

src/System.Private.CoreLib/shared/System/TimeSpan.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,14 @@ namespace System
2727
public readonly struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IFormattable, ISpanFormattable
2828
{
2929
public const long TicksPerMillisecond = 10000;
30-
private const double MillisecondsPerTick = 1.0 / TicksPerMillisecond;
3130

3231
public const long TicksPerSecond = TicksPerMillisecond * 1000; // 10,000,000
33-
private const double SecondsPerTick = 1.0 / TicksPerSecond; // 0.0000001
3432

3533
public const long TicksPerMinute = TicksPerSecond * 60; // 600,000,000
36-
private const double MinutesPerTick = 1.0 / TicksPerMinute; // 1.6666666666667e-9
3734

3835
public const long TicksPerHour = TicksPerMinute * 60; // 36,000,000,000
39-
private const double HoursPerTick = 1.0 / TicksPerHour; // 2.77777777777777778e-11
4036

4137
public const long TicksPerDay = TicksPerHour * 24; // 864,000,000,000
42-
private const double DaysPerTick = 1.0 / TicksPerDay; // 1.1574074074074074074e-12
4338

4439
private const int MillisPerSecond = 1000;
4540
private const int MillisPerMinute = MillisPerSecond * 60; // 60,000
@@ -118,19 +113,19 @@ public int Seconds
118113

119114
public double TotalDays
120115
{
121-
get { return ((double)_ticks) * DaysPerTick; }
116+
get { return ((double)_ticks) / TicksPerDay; }
122117
}
123118

124119
public double TotalHours
125120
{
126-
get { return (double)_ticks * HoursPerTick; }
121+
get { return (double)_ticks / TicksPerHour; }
127122
}
128123

129124
public double TotalMilliseconds
130125
{
131126
get
132127
{
133-
double temp = (double)_ticks * MillisecondsPerTick;
128+
double temp = (double)_ticks / TicksPerMillisecond;
134129
if (temp > MaxMilliSeconds)
135130
return (double)MaxMilliSeconds;
136131

@@ -143,12 +138,12 @@ public double TotalMilliseconds
143138

144139
public double TotalMinutes
145140
{
146-
get { return (double)_ticks * MinutesPerTick; }
141+
get { return (double)_ticks / TicksPerMinute; }
147142
}
148143

149144
public double TotalSeconds
150145
{
151-
get { return (double)_ticks * SecondsPerTick; }
146+
get { return (double)_ticks / TicksPerSecond; }
152147
}
153148

154149
public TimeSpan Add(TimeSpan ts)
@@ -238,10 +233,10 @@ private static TimeSpan Interval(double value, double scale)
238233
{
239234
if (double.IsNaN(value))
240235
throw new ArgumentException(SR.Arg_CannotBeNaN);
241-
double millis = value * scale;
242-
if ((millis > long.MaxValue) || (millis < long.MinValue))
236+
double ticks = value * scale;
237+
if ((ticks > long.MaxValue) || (ticks < long.MinValue))
243238
throw new OverflowException(SR.Overflow_TimeSpanTooLong);
244-
return new TimeSpan((long)millis);
239+
return new TimeSpan((long)ticks);
245240
}
246241

247242
public static TimeSpan FromMilliseconds(double value)

0 commit comments

Comments
 (0)