Skip to content

Commit

Permalink
Revert "Expose the useful constant values in TimeSpan (#103498)" (#10…
Browse files Browse the repository at this point in the history
…3704)

This reverts commit 5fed175.
  • Loading branch information
tarekgh committed Jun 19, 2024
1 parent f8e575c commit 4b111dc
Show file tree
Hide file tree
Showing 20 changed files with 193 additions and 289 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; // can't pull in the new TimeSpan constant because this builds in older CLR versions
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
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 % TimeSpan.TicksPerMillisecond);
return CreateDateTimeFromSystemTime(in time, fileTime % 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)TimeSpan.TicksPerDay;
ulong ticks = n * (ulong)TicksPerDay;

ticks += time.Hour * (ulong)TimeSpan.TicksPerHour;
ticks += time.Minute * (ulong)TimeSpan.TicksPerMinute;
ticks += time.Hour * (ulong)TicksPerHour;
ticks += time.Minute * (ulong)TicksPerMinute;
uint second = time.Second;
if (second <= 59)
{
ulong tmp = second * (uint)TimeSpan.TicksPerSecond + time.Milliseconds * (uint)TimeSpan.TicksPerMillisecond + hundredNanoSecond;
ulong tmp = second * (uint)TicksPerSecond + time.Milliseconds * (uint)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 += TimeSpan.TicksPerMinute - 1 | KindUtc;
ticks += 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 * TimeSpan.TicksPerMillisecond)
if (Math.Abs(preciseSystemTimeResult - systemTimeResult) <= 100 * 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 < TimeSpan.TicksPerDay - TimeSpan.TicksPerSecond, "Leap second cache validity window should be less than 23:59:59.");
Debug.Assert(LeapSecondCache.ValidityPeriodInTicks < TicksPerDay - 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 % TimeSpan.TicksPerMillisecond;
ulong hundredNanoSecondNow = fileTimeNow % 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 + (TimeSpan.TicksPerDay - TimeSpan.TicksPerSecond) - LeapSecondCache.ValidityPeriodInTicks;
fileTimeAtStartOfValidityWindow = fileTimeAtBeginningOfDay + (TicksPerDay - 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 + (TimeSpan.TicksPerDay - TimeSpan.TicksPerSecond) - LeapSecondCache.ValidityPeriodInTicks;
dotnetDateDataAtStartOfValidityWindow = CreateDateTimeFromSystemTime(systemTimeAtBeginningOfDay, 0)._dateData + (TicksPerDay - 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 = TimeSpan.TicksPerMinute * 5;
internal const ulong ValidityPeriodInTicks = TicksPerMinute * 5;

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

0 comments on commit 4b111dc

Please sign in to comment.