Skip to content

Commit

Permalink
Expose the constants in TimeSpan (#103993)
Browse files Browse the repository at this point in the history
Publicly expose the useful constants that `TimeSpan` had internally declared since they're rooted in hard reality and not changeable, and quite useful elsewhere. Everything is a `long` except `HoursPerDay` because that is useful in many non-TimeSpan contexts and rarely would overflow.

Added /// documentation for newly public constants and updated _System.Runtime.cs_ assembly API reference.

Use the newly exposed values in all the runtime places similar constants are declared in runtime **EXCEPT** did not use the `TimeSpan` constant in _EventLogInternal.cs_ since that source is also built for older frameworks, leave existing internal constant.

In _Calendar.cs_, use an `int` cast at the call-sites to `Add` method to prevent performance degradation, these calls are known not to overflow.

Fixes #94545
  • Loading branch information
IDisposable authored Jun 26, 2024
1 parent 9d008f4 commit 1d272e6
Show file tree
Hide file tree
Showing 20 changed files with 291 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ internal sealed class EventLogInternal : IDisposable, ISupportInitialize

private const string EventLogKey = "SYSTEM\\CurrentControlSet\\Services\\EventLog";
private const string eventLogMutexName = "netfxeventlog.1.0";
private const int SecondsPerDay = 60 * 60 * 24;
private const int SecondsPerDay = 60 * 60 * 24; // can't pull in the new TimeSpan constant because this builds in older CLR versions

private const int Flag_notifying = 0x1; // keeps track of whether we're notifying our listeners - to prevent double notifications
private const int Flag_forwards = 0x2; // whether the cache contains entries in forwards order (true) or backwards (false)
Expand Down
24 changes: 12 additions & 12 deletions src/libraries/System.Private.CoreLib/src/System/DateTime.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private static unsafe DateTime FromFileTimeLeapSecondsAware(ulong fileTime)
{
throw new ArgumentOutOfRangeException(nameof(fileTime), SR.ArgumentOutOfRange_DateTimeBadTicks);
}
return CreateDateTimeFromSystemTime(in time, fileTime % TicksPerMillisecond);
return CreateDateTimeFromSystemTime(in time, fileTime % TimeSpan.TicksPerMillisecond);
}

private static unsafe ulong ToFileTimeLeapSecondsAware(long ticks)
Expand Down Expand Up @@ -112,20 +112,20 @@ private static DateTime CreateDateTimeFromSystemTime(in Interop.Kernel32.SYSTEMT
ReadOnlySpan<uint> days = IsLeapYear((int)year) ? DaysToMonth366 : DaysToMonth365;
int month = time.Month - 1;
uint n = DaysToYear(year) + days[month] + time.Day - 1;
ulong ticks = n * (ulong)TicksPerDay;
ulong ticks = n * (ulong)TimeSpan.TicksPerDay;

ticks += time.Hour * (ulong)TicksPerHour;
ticks += time.Minute * (ulong)TicksPerMinute;
ticks += time.Hour * (ulong)TimeSpan.TicksPerHour;
ticks += time.Minute * (ulong)TimeSpan.TicksPerMinute;
uint second = time.Second;
if (second <= 59)
{
ulong tmp = second * (uint)TicksPerSecond + time.Milliseconds * (uint)TicksPerMillisecond + hundredNanoSecond;
ulong tmp = second * (uint)TimeSpan.TicksPerSecond + time.Milliseconds * (uint)TimeSpan.TicksPerMillisecond + hundredNanoSecond;
return new DateTime(ticks + tmp | KindUtc);
}

// we have a leap second, force it to last second in the minute as DateTime doesn't account for leap seconds in its calculation.
// we use the maxvalue from the milliseconds and the 100-nano seconds to avoid reporting two out of order 59 seconds
ticks += TicksPerMinute - 1 | KindUtc;
ticks += TimeSpan.TicksPerMinute - 1 | KindUtc;
return new DateTime(ticks);
}

Expand Down Expand Up @@ -166,7 +166,7 @@ private static unsafe bool GetSystemSupportsLeapSeconds()
((delegate* unmanaged[SuppressGCTransition]<long*, void>)pfnGetSystemTime)(&systemTimeResult);
((delegate* unmanaged[SuppressGCTransition]<long*, void>)pfnGetSystemTimePrecise)(&preciseSystemTimeResult);

if (Math.Abs(preciseSystemTimeResult - systemTimeResult) <= 100 * TicksPerMillisecond)
if (Math.Abs(preciseSystemTimeResult - systemTimeResult) <= 100 * TimeSpan.TicksPerMillisecond)
{
pfnGetSystemTime = pfnGetSystemTimePrecise; // use the precise version
break;
Expand Down Expand Up @@ -194,7 +194,7 @@ private static unsafe DateTime UpdateLeapSecondCacheAndReturnUtcNow()
// cache will return incorrect values.

Debug.Assert(SystemSupportsLeapSeconds);
Debug.Assert(LeapSecondCache.ValidityPeriodInTicks < TicksPerDay - TicksPerSecond, "Leap second cache validity window should be less than 23:59:59.");
Debug.Assert(LeapSecondCache.ValidityPeriodInTicks < TimeSpan.TicksPerDay - TimeSpan.TicksPerSecond, "Leap second cache validity window should be less than 23:59:59.");

ulong fileTimeNow;
LeapSecondCache.s_pfnGetSystemTimeAsFileTime(&fileTimeNow);
Expand All @@ -203,7 +203,7 @@ private static unsafe DateTime UpdateLeapSecondCacheAndReturnUtcNow()
// First, convert the FILETIME to a SYSTEMTIME.

Interop.Kernel32.SYSTEMTIME systemTimeNow;
ulong hundredNanoSecondNow = fileTimeNow % TicksPerMillisecond;
ulong hundredNanoSecondNow = fileTimeNow % TimeSpan.TicksPerMillisecond;

// We need the FILETIME and the SYSTEMTIME to reflect each other's values.
// If FileTimeToSystemTime fails, call GetSystemTime and try again until it succeeds.
Expand Down Expand Up @@ -267,7 +267,7 @@ private static unsafe DateTime UpdateLeapSecondCacheAndReturnUtcNow()
}

// StartOfValidityWindow = MidnightUtc + 23:59:59 - ValidityPeriod
fileTimeAtStartOfValidityWindow = fileTimeAtBeginningOfDay + (TicksPerDay - TicksPerSecond) - LeapSecondCache.ValidityPeriodInTicks;
fileTimeAtStartOfValidityWindow = fileTimeAtBeginningOfDay + (TimeSpan.TicksPerDay - TimeSpan.TicksPerSecond) - LeapSecondCache.ValidityPeriodInTicks;
if (fileTimeNow - fileTimeAtStartOfValidityWindow >= LeapSecondCache.ValidityPeriodInTicks)
{
// If we're inside this block, then we slid the validity window back so far that the current time is no
Expand Down Expand Up @@ -295,7 +295,7 @@ private static unsafe DateTime UpdateLeapSecondCacheAndReturnUtcNow()
return CreateDateTimeFromSystemTime(systemTimeNow, hundredNanoSecondNow);
}

dotnetDateDataAtStartOfValidityWindow = CreateDateTimeFromSystemTime(systemTimeAtBeginningOfDay, 0)._dateData + (TicksPerDay - TicksPerSecond) - LeapSecondCache.ValidityPeriodInTicks;
dotnetDateDataAtStartOfValidityWindow = CreateDateTimeFromSystemTime(systemTimeAtBeginningOfDay, 0)._dateData + (TimeSpan.TicksPerDay - TimeSpan.TicksPerSecond) - LeapSecondCache.ValidityPeriodInTicks;
}

// Finally, update the cache and return UtcNow.
Expand Down Expand Up @@ -330,7 +330,7 @@ static DateTime LowGranularityNonCachedFallback()
private sealed class LeapSecondCache
{
// The length of the validity window. Must be less than 23:59:59.
internal const ulong ValidityPeriodInTicks = TicksPerMinute * 5;
internal const ulong ValidityPeriodInTicks = TimeSpan.TicksPerMinute * 5;

// The FILETIME value at the beginning of the validity window.
internal ulong OSFileTimeTicksAtStartOfValidityWindow;
Expand Down
Loading

0 comments on commit 1d272e6

Please sign in to comment.