@@ -140,7 +140,9 @@ public LocalDate(Era era, int yearOfEra, int month, int day, CalendarSystem cale
140
140
141
141
/// <summary>Gets the calendar system associated with this local date.</summary>
142
142
/// <value>The calendar system associated with this local date.</value>
143
- public CalendarSystem Calendar => CalendarSystem . ForOrdinal ( yearMonthDayCalendar . CalendarOrdinal ) ;
143
+ public CalendarSystem Calendar => CalendarSystem . ForOrdinal ( CalendarOrdinal ) ;
144
+
145
+ private CalendarOrdinal CalendarOrdinal => yearMonthDayCalendar . CalendarOrdinal ;
144
146
145
147
/// <summary>Gets the year of this local date.</summary>
146
148
/// <remarks>This returns the "absolute year", so, for the ISO calendar,
@@ -445,8 +447,8 @@ public static LocalDate FromYearMonthWeekAndDay(int year, int month, int occurre
445
447
/// <returns>true if the <paramref name="lhs"/> is strictly earlier than <paramref name="rhs"/>, false otherwise.</returns>
446
448
public static bool operator < ( LocalDate lhs , LocalDate rhs )
447
449
{
448
- Preconditions . CheckArgument ( lhs . Calendar . Equals ( rhs . Calendar ) , nameof ( rhs ) , "Only values in the same calendar can be compared" ) ;
449
- return lhs . CompareTo ( rhs ) < 0 ;
450
+ Preconditions . CheckArgument ( lhs . CalendarOrdinal == rhs . CalendarOrdinal , nameof ( rhs ) , "Only values in the same calendar can be compared" ) ;
451
+ return lhs . TrustedCompareTo ( rhs ) < 0 ;
450
452
}
451
453
452
454
/// <summary>
@@ -460,8 +462,8 @@ public static LocalDate FromYearMonthWeekAndDay(int year, int month, int occurre
460
462
/// <returns>true if the <paramref name="lhs"/> is earlier than or equal to <paramref name="rhs"/>, false otherwise.</returns>
461
463
public static bool operator <= ( LocalDate lhs , LocalDate rhs )
462
464
{
463
- Preconditions . CheckArgument ( lhs . Calendar . Equals ( rhs . Calendar ) , nameof ( rhs ) , "Only values in the same calendar can be compared" ) ;
464
- return lhs . CompareTo ( rhs ) <= 0 ;
465
+ Preconditions . CheckArgument ( lhs . CalendarOrdinal == rhs . CalendarOrdinal , nameof ( rhs ) , "Only values in the same calendar can be compared" ) ;
466
+ return lhs . TrustedCompareTo ( rhs ) <= 0 ;
465
467
}
466
468
467
469
/// <summary>
@@ -475,8 +477,8 @@ public static LocalDate FromYearMonthWeekAndDay(int year, int month, int occurre
475
477
/// <returns>true if the <paramref name="lhs"/> is strictly later than <paramref name="rhs"/>, false otherwise.</returns>
476
478
public static bool operator > ( LocalDate lhs , LocalDate rhs )
477
479
{
478
- Preconditions . CheckArgument ( lhs . Calendar . Equals ( rhs . Calendar ) , nameof ( rhs ) , "Only values in the same calendar can be compared" ) ;
479
- return lhs . CompareTo ( rhs ) > 0 ;
480
+ Preconditions . CheckArgument ( lhs . CalendarOrdinal == rhs . CalendarOrdinal , nameof ( rhs ) , "Only values in the same calendar can be compared" ) ;
481
+ return lhs . TrustedCompareTo ( rhs ) > 0 ;
480
482
}
481
483
482
484
/// <summary>
@@ -490,8 +492,8 @@ public static LocalDate FromYearMonthWeekAndDay(int year, int month, int occurre
490
492
/// <returns>true if the <paramref name="lhs"/> is later than or equal to <paramref name="rhs"/>, false otherwise.</returns>
491
493
public static bool operator >= ( LocalDate lhs , LocalDate rhs )
492
494
{
493
- Preconditions . CheckArgument ( lhs . Calendar . Equals ( rhs . Calendar ) , nameof ( rhs ) , "Only values in the same calendar can be compared" ) ;
494
- return lhs . CompareTo ( rhs ) >= 0 ;
495
+ Preconditions . CheckArgument ( lhs . CalendarOrdinal == rhs . CalendarOrdinal , nameof ( rhs ) , "Only values in the same calendar can be compared" ) ;
496
+ return lhs . TrustedCompareTo ( rhs ) >= 0 ;
495
497
}
496
498
497
499
/// <summary>
@@ -506,10 +508,16 @@ public static LocalDate FromYearMonthWeekAndDay(int year, int month, int occurre
506
508
/// later than <paramref name="other"/>.</returns>
507
509
public int CompareTo ( LocalDate other )
508
510
{
509
- Preconditions . CheckArgument ( Calendar . Equals ( other . Calendar ) , nameof ( other ) , "Only values with the same calendar system can be compared" ) ;
510
- return Calendar . Compare ( YearMonthDay , other . YearMonthDay ) ;
511
+ Preconditions . CheckArgument ( CalendarOrdinal == other . CalendarOrdinal , nameof ( other ) , "Only values with the same calendar system can be compared" ) ;
512
+ return TrustedCompareTo ( other ) ;
511
513
}
512
514
515
+ /// <summary>
516
+ /// Performs a comparison with another date, trusting that the calendar of the other date is already correct.
517
+ /// This avoids duplicate calendar checks.
518
+ /// </summary>
519
+ private int TrustedCompareTo ( [ Trusted ] LocalDate other ) => Calendar . Compare ( YearMonthDay , other . YearMonthDay ) ;
520
+
513
521
/// <summary>
514
522
/// Implementation of <see cref="IComparable.CompareTo"/> to compare two LocalDates.
515
523
/// See the type documentation for a description of ordering semantics.
0 commit comments