mirror of
https://github.com/Microsoft/calculator.git
synced 2025-07-12 08:16:00 -07:00
Date difference: display the difference in days (only) when we aren't able to calculate the difference in days/weeks/months.. (#555)
* Display date difference in days if can't display in days/weeks/months/... * add comments * remove not used variable totalDaysDiff * improve UpdateDisplayResult * Display error message when the calculator can't calculate the difference between 2 dates
This commit is contained in:
parent
25708f0bef
commit
2a5a52d44d
5 changed files with 68 additions and 21 deletions
|
@ -119,7 +119,7 @@ bool DateCalculationEngine::SubtractDuration(_In_ DateTime startDate, _In_ const
|
|||
}
|
||||
|
||||
// Calculate the difference between two dates
|
||||
void DateCalculationEngine::GetDateDifference(_In_ DateTime date1, _In_ DateTime date2, _In_ DateUnit outputFormat, _Out_ DateDifference* difference)
|
||||
bool DateCalculationEngine::TryGetDateDifference(_In_ DateTime date1, _In_ DateTime date2, _In_ DateUnit outputFormat, _Out_ DateDifference* difference)
|
||||
{
|
||||
DateTime startDate;
|
||||
DateTime endDate;
|
||||
|
@ -176,8 +176,9 @@ void DateCalculationEngine::GetDateDifference(_In_ DateTime date1, _In_ DateTime
|
|||
catch (Platform::InvalidArgumentException ^)
|
||||
{
|
||||
// Operation failed due to out of bound result
|
||||
// Do nothing
|
||||
differenceInDates[unitIndex] = 0;
|
||||
// For example: 31st Dec, 9999 - last valid date
|
||||
*difference = DateDifferenceUnknown;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -190,6 +191,12 @@ void DateCalculationEngine::GetDateDifference(_In_ DateTime date1, _In_ DateTime
|
|||
if (tempDaysDiff < 0)
|
||||
{
|
||||
// pivotDate has gone over the end date; start from the beginning of this unit
|
||||
if (differenceInDates[unitIndex] == 0)
|
||||
{
|
||||
// differenceInDates[unitIndex] is unsigned, the value can't be negative
|
||||
*difference = DateDifferenceUnknown;
|
||||
return false;
|
||||
}
|
||||
differenceInDates[unitIndex] -= 1;
|
||||
pivotDate = tempPivotDate;
|
||||
pivotDate = AdjustCalendarDate(pivotDate, dateUnit, static_cast<int>(differenceInDates[unitIndex]));
|
||||
|
@ -211,16 +218,25 @@ void DateCalculationEngine::GetDateDifference(_In_ DateTime date1, _In_ DateTime
|
|||
}
|
||||
catch (Platform::InvalidArgumentException ^)
|
||||
{
|
||||
// handling for 31st Dec, 9999 last valid date
|
||||
// Do nothing - break out
|
||||
break;
|
||||
// Operation failed due to out of bound result
|
||||
// For example: 31st Dec, 9999 - last valid date
|
||||
*difference = DateDifferenceUnknown;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} while (tempDaysDiff != 0); // dates are the same - exit the loop
|
||||
|
||||
tempPivotDate = AdjustCalendarDate(tempPivotDate, dateUnit, static_cast<int>(differenceInDates[unitIndex]));
|
||||
pivotDate = tempPivotDate;
|
||||
daysDiff = GetDifferenceInDays(pivotDate, endDate);
|
||||
int signedDaysDiff = GetDifferenceInDays(pivotDate, endDate);
|
||||
if (signedDaysDiff < 0)
|
||||
{
|
||||
// daysDiff is unsigned, the value can't be negative
|
||||
*difference = DateDifferenceUnknown;
|
||||
return false;
|
||||
}
|
||||
|
||||
daysDiff = signedDaysDiff;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -232,6 +248,7 @@ void DateCalculationEngine::GetDateDifference(_In_ DateTime date1, _In_ DateTime
|
|||
difference->month = differenceInDates[1];
|
||||
difference->week = differenceInDates[2];
|
||||
difference->day = differenceInDates[3];
|
||||
return true;
|
||||
}
|
||||
|
||||
// Private Methods
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue