Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the useful constant values in TimeSpan #103498

Merged
merged 5 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading