DateDiff: Optimize how we build the string used when we calculate the difference between 2 dates (#195)
Optimize how we build the result of GetDateDiffString and GetDateDiffStringInDays, using std::wstring (mutuable) instead of Platform::String (immutable)
This commit is contained in:
parent
244fd8deee
commit
ca15f05227
@ -73,7 +73,7 @@ DateCalculatorViewModel::DateCalculatorViewModel() :
|
|||||||
|
|
||||||
// Initialize the list separator delimiter appended with a space at the end, e.g. ", "
|
// Initialize the list separator delimiter appended with a space at the end, e.g. ", "
|
||||||
// This will be used for date difference formatting: Y years, M months, W weeks, D days
|
// This will be used for date difference formatting: Y years, M months, W weeks, D days
|
||||||
m_listSeparator = ref new String((localizationSettings.GetListSeparator() + L" ").c_str());
|
m_listSeparator = localizationSettings.GetListSeparator() + L" ";
|
||||||
|
|
||||||
// Initialize the output results
|
// Initialize the output results
|
||||||
UpdateDisplayResult();
|
UpdateDisplayResult();
|
||||||
@ -181,8 +181,8 @@ void DateCalculatorViewModel::UpdateDisplayResult()
|
|||||||
StrDateDiffResult = AppResourceProvider::GetInstance().GetResourceString(L"Date_SameDates");
|
StrDateDiffResult = AppResourceProvider::GetInstance().GetResourceString(L"Date_SameDates");
|
||||||
}
|
}
|
||||||
else if ((m_dateDiffResult.year == 0) &&
|
else if ((m_dateDiffResult.year == 0) &&
|
||||||
(m_dateDiffResult.month == 0) &&
|
(m_dateDiffResult.month == 0) &&
|
||||||
(m_dateDiffResult.week == 0))
|
(m_dateDiffResult.week == 0))
|
||||||
{
|
{
|
||||||
IsDiffInDays = true;
|
IsDiffInDays = true;
|
||||||
StrDateDiffResultInDays = L"";
|
StrDateDiffResultInDays = L"";
|
||||||
@ -245,22 +245,23 @@ void DateCalculatorViewModel::InitializeDateOutputFormats(_In_ String^ calendarI
|
|||||||
|
|
||||||
String^ DateCalculatorViewModel::GetDateDiffString() const
|
String^ DateCalculatorViewModel::GetDateDiffString() const
|
||||||
{
|
{
|
||||||
String^ result = L"";
|
wstring result;
|
||||||
bool addDelimiter = false;
|
bool addDelimiter = false;
|
||||||
AppResourceProvider resourceLoader = AppResourceProvider::GetInstance();
|
AppResourceProvider resourceLoader = AppResourceProvider::GetInstance();
|
||||||
|
|
||||||
auto yearCount = m_dateDiffResult.year;
|
auto yearCount = m_dateDiffResult.year;
|
||||||
if (yearCount > 0)
|
if (yearCount > 0)
|
||||||
{
|
{
|
||||||
result = String::Concat(GetLocalizedNumberString(yearCount), L" ");
|
result += GetLocalizedNumberString(yearCount)->Data();
|
||||||
|
result += L" ";
|
||||||
|
|
||||||
if (yearCount > 1)
|
if (yearCount > 1)
|
||||||
{
|
{
|
||||||
result = String::Concat(result, resourceLoader.GetResourceString(L"Date_Years"));
|
result += resourceLoader.GetResourceString(L"Date_Years")->Data();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
result = String::Concat(result, resourceLoader.GetResourceString(L"Date_Year"));
|
result += resourceLoader.GetResourceString(L"Date_Year")->Data();
|
||||||
}
|
}
|
||||||
|
|
||||||
// set the flags to add a delimiter whenever the next unit is added
|
// set the flags to add a delimiter whenever the next unit is added
|
||||||
@ -272,22 +273,23 @@ String^ DateCalculatorViewModel::GetDateDiffString() const
|
|||||||
{
|
{
|
||||||
if (addDelimiter)
|
if (addDelimiter)
|
||||||
{
|
{
|
||||||
result = String::Concat(result, m_listSeparator);
|
result += m_listSeparator;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
addDelimiter = true;
|
addDelimiter = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = String::Concat(result, String::Concat(GetLocalizedNumberString(monthCount), L" "));
|
result += GetLocalizedNumberString(monthCount)->Data();
|
||||||
|
result += L" ";
|
||||||
|
|
||||||
if (monthCount > 1)
|
if (monthCount > 1)
|
||||||
{
|
{
|
||||||
result = String::Concat(result, resourceLoader.GetResourceString(L"Date_Months"));
|
result += resourceLoader.GetResourceString(L"Date_Months")->Data();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
result = String::Concat(result, resourceLoader.GetResourceString(L"Date_Month"));
|
result += resourceLoader.GetResourceString(L"Date_Month")->Data();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -296,22 +298,23 @@ String^ DateCalculatorViewModel::GetDateDiffString() const
|
|||||||
{
|
{
|
||||||
if (addDelimiter)
|
if (addDelimiter)
|
||||||
{
|
{
|
||||||
result = String::Concat(result, m_listSeparator);
|
result += m_listSeparator;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
addDelimiter = true;
|
addDelimiter = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = String::Concat(result, String::Concat(GetLocalizedNumberString(weekCount), L" "));
|
result += GetLocalizedNumberString(weekCount)->Data();
|
||||||
|
result += L" ";
|
||||||
|
|
||||||
if (weekCount > 1)
|
if (weekCount > 1)
|
||||||
{
|
{
|
||||||
result = String::Concat(result, resourceLoader.GetResourceString(L"Date_Weeks"));
|
result += resourceLoader.GetResourceString(L"Date_Weeks")->Data();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
result = String::Concat(result, resourceLoader.GetResourceString(L"Date_Week"));
|
result += resourceLoader.GetResourceString(L"Date_Week")->Data();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -320,43 +323,45 @@ String^ DateCalculatorViewModel::GetDateDiffString() const
|
|||||||
{
|
{
|
||||||
if (addDelimiter)
|
if (addDelimiter)
|
||||||
{
|
{
|
||||||
result = String::Concat(result, m_listSeparator);
|
result += m_listSeparator;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
addDelimiter = true;
|
addDelimiter = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = String::Concat(result, String::Concat(GetLocalizedNumberString(dayCount), L" "));
|
result += GetLocalizedNumberString(dayCount)->Data();
|
||||||
|
result += L" ";
|
||||||
|
|
||||||
if (dayCount > 1)
|
if (dayCount > 1)
|
||||||
{
|
{
|
||||||
result = String::Concat(result, resourceLoader.GetResourceString(L"Date_Days"));
|
result += resourceLoader.GetResourceString(L"Date_Days")->Data();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
result = String::Concat(result, resourceLoader.GetResourceString(L"Date_Day"));
|
result += resourceLoader.GetResourceString(L"Date_Day")->Data();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return ref new String(result.data());
|
||||||
}
|
}
|
||||||
|
|
||||||
String^ DateCalculatorViewModel::GetDateDiffStringInDays() const
|
String^ DateCalculatorViewModel::GetDateDiffStringInDays() const
|
||||||
{
|
{
|
||||||
String^ strDateUnit;
|
wstring result = GetLocalizedNumberString(m_dateDiffResultInDays.day)->Data();
|
||||||
|
result += L" ";
|
||||||
|
|
||||||
// Display the result as '1 day' or 'N days'
|
// Display the result as '1 day' or 'N days'
|
||||||
if (m_dateDiffResultInDays.day > 1)
|
if (m_dateDiffResultInDays.day > 1)
|
||||||
{
|
{
|
||||||
strDateUnit = AppResourceProvider::GetInstance().GetResourceString(L"Date_Days");
|
result += AppResourceProvider::GetInstance().GetResourceString(L"Date_Days")->Data();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
strDateUnit = AppResourceProvider::GetInstance().GetResourceString(L"Date_Day");
|
result += AppResourceProvider::GetInstance().GetResourceString(L"Date_Day")->Data();
|
||||||
}
|
}
|
||||||
|
|
||||||
return String::Concat(GetLocalizedNumberString(m_dateDiffResultInDays.day), String::Concat(L" ", strDateUnit));
|
return ref new String(result.data());
|
||||||
}
|
}
|
||||||
|
|
||||||
void DateCalculatorViewModel::OnCopyCommand(Platform::Object^ parameter)
|
void DateCalculatorViewModel::OnCopyCommand(Platform::Object^ parameter)
|
||||||
|
@ -146,7 +146,7 @@ namespace CalculatorApp
|
|||||||
CalculatorApp::Common::DateCalculation::DateUnit m_daysOutputFormat;
|
CalculatorApp::Common::DateCalculation::DateUnit m_daysOutputFormat;
|
||||||
CalculatorApp::Common::DateCalculation::DateUnit m_allDateUnitsOutputFormat;
|
CalculatorApp::Common::DateCalculation::DateUnit m_allDateUnitsOutputFormat;
|
||||||
Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ m_dateTimeFormatter;
|
Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ m_dateTimeFormatter;
|
||||||
Platform::String^ m_listSeparator;
|
std::wstring m_listSeparator;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||||
// Licensed under the MIT License.
|
// Licensed under the MIT License.
|
||||||
|
|
||||||
#include "pch.h"
|
#include "pch.h"
|
||||||
@ -494,6 +494,26 @@ namespace DateCalculationUnitTests
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(DateCalcViewModelDateDiffIgnoreSignTest)
|
||||||
|
{
|
||||||
|
auto viewModel = ref new DateCalculatorViewModel();
|
||||||
|
|
||||||
|
viewModel->IsDateDiffMode = true;
|
||||||
|
VERIFY_IS_TRUE(viewModel->IsDateDiffMode);
|
||||||
|
|
||||||
|
viewModel->FromDate = DateUtils::SystemTimeToDateTime(date[10]);
|
||||||
|
viewModel->ToDate = DateUtils::SystemTimeToDateTime(date[6]);
|
||||||
|
|
||||||
|
VERIFY_IS_FALSE(viewModel->IsDiffInDays);
|
||||||
|
VERIFY_ARE_EQUAL(StringReference(L"305 days"), viewModel->StrDateDiffResultInDays);
|
||||||
|
VERIFY_ARE_EQUAL(StringReference(L"10 months"), viewModel->StrDateDiffResult);
|
||||||
|
viewModel->FromDate = DateUtils::SystemTimeToDateTime(date[6]);
|
||||||
|
viewModel->ToDate = DateUtils::SystemTimeToDateTime(date[10]);
|
||||||
|
VERIFY_IS_FALSE(viewModel->IsDiffInDays);
|
||||||
|
VERIFY_ARE_EQUAL(StringReference(L"305 days"), viewModel->StrDateDiffResultInDays);
|
||||||
|
VERIFY_ARE_EQUAL(StringReference(L"10 months"), viewModel->StrDateDiffResult);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_METHOD(DateCalcViewModelDateDiffTest)
|
TEST_METHOD(DateCalcViewModelDateDiffTest)
|
||||||
{
|
{
|
||||||
// TODO - MSFT 10331900, fix this test
|
// TODO - MSFT 10331900, fix this test
|
||||||
@ -516,7 +536,23 @@ namespace DateCalculationUnitTests
|
|||||||
//VERIFY_ARE_EQUAL(StringReference(L"8398 years, 11 months, 4 weeks, 2 days"), viewModel->StrDateDiffResult);
|
//VERIFY_ARE_EQUAL(StringReference(L"8398 years, 11 months, 4 weeks, 2 days"), viewModel->StrDateDiffResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_METHOD(DateCalcViewModelDateDiffResultInDaysTest)
|
TEST_METHOD(DateCalcViewModelDateDiffResultInPositiveDaysTest)
|
||||||
|
{
|
||||||
|
auto viewModel = ref new DateCalculatorViewModel();
|
||||||
|
|
||||||
|
viewModel->IsDateDiffMode = true;
|
||||||
|
VERIFY_IS_TRUE(viewModel->IsDateDiffMode);
|
||||||
|
|
||||||
|
viewModel->FromDate = DateUtils::SystemTimeToDateTime(date[1]);
|
||||||
|
viewModel->ToDate = DateUtils::SystemTimeToDateTime(date[0]);
|
||||||
|
|
||||||
|
// Assert for the result
|
||||||
|
VERIFY_IS_TRUE(viewModel->IsDiffInDays);
|
||||||
|
VERIFY_ARE_EQUAL(StringReference(L"1 day"), viewModel->StrDateDiffResult);
|
||||||
|
VERIFY_IS_NULL(viewModel->StrDateDiffResultInDays);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(DateCalcViewModelDateDiffFromDateHigherThanToDate)
|
||||||
{
|
{
|
||||||
auto viewModel = ref new DateCalculatorViewModel();
|
auto viewModel = ref new DateCalculatorViewModel();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user