Skip to content

Commit

Permalink
Cleanup some DateTime related constants (#105071)
Browse files Browse the repository at this point in the history
  • Loading branch information
lilinus authored and pull[bot] committed Sep 6, 2024
1 parent 7a69216 commit 5c15370
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/DateOnly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public readonly struct DateOnly
// Maps to Jan 1st year 1
private const int MinDayNumber = 0;

// Maps to December 31 year 9999. The value calculated from "new DateTime(9999, 12, 31).Ticks / TimeSpan.TicksPerDay"
private const int MaxDayNumber = 3_652_058;
// Maps to December 31 year 9999.
private const int MaxDayNumber = DateTime.DaysTo10000 - 1;

private static int DayNumberFromDateTime(DateTime dt) => (int)((ulong)dt.Ticks / TimeSpan.TicksPerDay);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public readonly partial struct DateTime
// Number of days from 1/1/0001 to 12/31/1969
internal const int DaysTo1970 = DaysPer400Years * 4 + DaysPer100Years * 3 + DaysPer4Years * 17 + DaysPerYear; // 719,162
// Number of days from 1/1/0001 to 12/31/9999
private const int DaysTo10000 = DaysPer400Years * 25 - 366; // 3652059
internal const int DaysTo10000 = DaysPer400Years * 25 - 366; // 3652059

internal const long MinTicks = 0;
internal const long MaxTicks = DaysTo10000 * TimeSpan.TicksPerDay - 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,7 @@ namespace System.Globalization

public abstract class Calendar : ICloneable
{
// Number of days in a non-leap year
internal const int DaysPerYear = 365;
// Number of days in 4 years
internal const int DaysPer4Years = DaysPerYear * 4 + 1;
// Number of days in 100 years
internal const int DaysPer100Years = DaysPer4Years * 25 - 1;
// Number of days in 400 years
internal const int DaysPer400Years = DaysPer100Years * 4 + 1;

// Number of days from 1/1/0001 to 1/1/10000
internal const int DaysTo10000 = DaysPer400Years * 25 - 366;

internal const long MaxMillis = (long)DaysTo10000 * TimeSpan.MillisecondsPerDay;
internal const long MaxMillis = (long)DateTime.DaysTo10000 * TimeSpan.MillisecondsPerDay;

private int _currentEraValue = -1;

Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/TimeOnly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public readonly struct TimeOnly
// MinTimeTicks is the ticks for the midnight time 00:00:00.000 AM
private const long MinTimeTicks = 0;

// MaxTimeTicks is the max tick value for the time in the day. It is calculated using DateTime.Today.AddTicks(-1).TimeOfDay.Ticks.
private const long MaxTimeTicks = 863_999_999_999;
// MaxTimeTicks is the max tick value for the time in the day.
private const long MaxTimeTicks = TimeSpan.TicksPerDay - 1;

/// <summary>
/// Represents the smallest possible value of TimeOnly.
Expand Down

0 comments on commit 5c15370

Please sign in to comment.