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:
Rudy Huyn
2019-06-30 18:22:29 -07:00
committed by Eric Wong
parent 25708f0bef
commit 2a5a52d44d
5 changed files with 68 additions and 21 deletions

View File

@@ -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

View File

@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
@@ -35,8 +35,15 @@ namespace CalculatorApp
int month = 0;
int week = 0;
int day = 0;
bool operator==(const DateDifference& dd) const
{
return year == dd.year && month == dd.month && week == dd.week && day == day;
}
};
const DateDifference DateDifferenceUnknown{ INT_MIN, INT_MIN, INT_MIN, INT_MIN };
class DateCalculationEngine
{
public:
@@ -50,7 +57,7 @@ namespace CalculatorApp
_In_ Windows::Foundation::DateTime startDate,
_In_ const DateDifference& duration,
_Out_ Windows::Foundation::DateTime* endDate);
void __nothrow GetDateDifference(
bool __nothrow TryGetDateDifference(
_In_ Windows::Foundation::DateTime date1,
_In_ Windows::Foundation::DateTime date2,
_In_ DateUnit outputFormat,