@@ -146,6 +146,7 @@ IBox<DateDifference> ^ DateCalculationEngine::TryGetDateDifference(_In_ DateTime
146146 endDate = date1;
147147 }
148148
149+
149150 pivotDate = startDate;
150151
151152 daysDiff = GetDifferenceInDays (startDate, endDate);
@@ -158,7 +159,20 @@ IBox<DateDifference> ^ DateCalculationEngine::TryGetDateDifference(_In_ DateTime
158159 UINT approximateDaysInYear;
159160
160161 // If we're unable to calculate the days-in-month or days-in-year, we'll leave the values at 0.
161- if (TryGetCalendarDaysInMonth (startDate, daysInMonth) && TryGetCalendarDaysInYear (endDate, approximateDaysInYear))
162+ bool gotDaysInMonth = TryGetCalendarDaysInMonth (startDate, daysInMonth);
163+ bool gotDaysInYear = TryGetCalendarDaysInYear (endDate, approximateDaysInYear);
164+
165+ // Fallback for calendar functions that might fail at boundary dates
166+ if (!gotDaysInMonth) {
167+ // Use a reasonable default for days in month
168+ daysInMonth = 31 ;
169+ }
170+ if (!gotDaysInYear) {
171+ // Use a reasonable default for days in year
172+ approximateDaysInYear = 365 ;
173+ }
174+
175+ if (gotDaysInMonth || gotDaysInYear)
162176 {
163177 UINT daysIn[c_unitsOfDate] = { approximateDaysInYear, daysInMonth, c_daysInWeek, 1 };
164178
@@ -178,7 +192,17 @@ IBox<DateDifference> ^ DateCalculationEngine::TryGetDateDifference(_In_ DateTime
178192 {
179193 try
180194 {
181- pivotDate = AdjustCalendarDate (pivotDate, dateUnit, static_cast <int >(differenceInDates[unitIndex]));
195+ // For very large differences, try to add in smaller chunks to avoid overflow
196+ int remainingUnits = static_cast <int >(differenceInDates[unitIndex]);
197+ DateTime tempPivot = pivotDate;
198+
199+ while (remainingUnits > 0 )
200+ {
201+ int chunkSize = min (remainingUnits, 1000 ); // Add at most 1000 units at a time
202+ tempPivot = AdjustCalendarDate (tempPivot, dateUnit, chunkSize);
203+ remainingUnits -= chunkSize;
204+ }
205+ pivotDate = tempPivot;
182206 }
183207 catch (Platform::InvalidArgumentException ^)
184208 {
@@ -204,7 +228,15 @@ IBox<DateDifference> ^ DateCalculationEngine::TryGetDateDifference(_In_ DateTime
204228 }
205229 differenceInDates[unitIndex] -= 1 ;
206230 pivotDate = tempPivotDate;
207- pivotDate = AdjustCalendarDate (pivotDate, dateUnit, static_cast <int >(differenceInDates[unitIndex]));
231+
232+ // Use chunked approach for large values
233+ int remainingUnits = static_cast <int >(differenceInDates[unitIndex]);
234+ while (remainingUnits > 0 )
235+ {
236+ int chunkSize = min (remainingUnits, 1000 );
237+ pivotDate = AdjustCalendarDate (pivotDate, dateUnit, chunkSize);
238+ remainingUnits -= chunkSize;
239+ }
208240 isEndDateHit = true ;
209241 }
210242 else if (tempDaysDiff > 0 )
@@ -230,7 +262,16 @@ IBox<DateDifference> ^ DateCalculationEngine::TryGetDateDifference(_In_ DateTime
230262 }
231263 } while (tempDaysDiff != 0 ); // dates are the same - exit the loop
232264
233- tempPivotDate = AdjustCalendarDate (tempPivotDate, dateUnit, static_cast <int >(differenceInDates[unitIndex]));
265+ // Use chunked approach for large values
266+ int remainingUnits = static_cast <int >(differenceInDates[unitIndex]);
267+ DateTime chunkPivot = tempPivotDate;
268+ while (remainingUnits > 0 )
269+ {
270+ int chunkSize = min (remainingUnits, 1000 );
271+ chunkPivot = AdjustCalendarDate (chunkPivot, dateUnit, chunkSize);
272+ remainingUnits -= chunkSize;
273+ }
274+ tempPivotDate = chunkPivot;
234275 pivotDate = tempPivotDate;
235276 int signedDaysDiff = GetDifferenceInDays (pivotDate, endDate);
236277 if (signedDaysDiff < 0 )
@@ -252,6 +293,8 @@ IBox<DateDifference> ^ DateCalculationEngine::TryGetDateDifference(_In_ DateTime
252293 result.month = differenceInDates[1 ];
253294 result.week = differenceInDates[2 ];
254295 result.day = differenceInDates[3 ];
296+
297+
255298 return result;
256299}
257300
0 commit comments