Skip to content

Commit

Permalink
Expose the useful constant values in TimeSpan
Browse files Browse the repository at this point in the history
Make the useful constants that TimeSpan has internally declare public because they're rooted in hard reality and not changeable, and quite useful elsewhere.
Update reference assembly and use the newly exposed values in all  the places in Calendar and DateTime and related classes.
Fixes #94545
  • Loading branch information
IDisposable committed Jun 15, 2024
1 parent 4a87c0e commit 40d4350
Show file tree
Hide file tree
Showing 20 changed files with 163 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ 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 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 Expand Up @@ -317,7 +316,7 @@ public int MinimumRetentionDays
if (retention == 0 || retention == -1)
return retention;
else
return (int)(((double)retention) / SecondsPerDay);
return (int)(((double)retention) / TimeSpan.SecondsPerDay);
}

return 7;
Expand Down Expand Up @@ -1030,7 +1029,7 @@ public void ModifyOverflowPolicy(OverflowAction action, int retentionDays)
if (retentionDays < 1 || retentionDays > 365)
throw new ArgumentOutOfRangeException(SR.RentionDaysOutOfRange);

retentionvalue = (long)retentionDays * SecondsPerDay;
retentionvalue = (long)retentionDays * TimeSpan.SecondsPerDay;
}

using (RegistryKey logkey = GetLogRegKey(currentMachineName, true))
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 40d4350

Please sign in to comment.