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:
Rudy Huyn 2019-03-15 17:20:33 -07:00 committed by Daniel Belcher
parent 244fd8deee
commit ca15f05227
3 changed files with 68 additions and 27 deletions

View File

@ -73,7 +73,7 @@ DateCalculatorViewModel::DateCalculatorViewModel() :
// 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
m_listSeparator = ref new String((localizationSettings.GetListSeparator() + L" ").c_str());
m_listSeparator = localizationSettings.GetListSeparator() + L" ";
// Initialize the output results
UpdateDisplayResult();
@ -181,8 +181,8 @@ void DateCalculatorViewModel::UpdateDisplayResult()
StrDateDiffResult = AppResourceProvider::GetInstance().GetResourceString(L"Date_SameDates");
}
else if ((m_dateDiffResult.year == 0) &&
(m_dateDiffResult.month == 0) &&
(m_dateDiffResult.week == 0))
(m_dateDiffResult.month == 0) &&
(m_dateDiffResult.week == 0))
{
IsDiffInDays = true;
StrDateDiffResultInDays = L"";
@ -245,22 +245,23 @@ void DateCalculatorViewModel::InitializeDateOutputFormats(_In_ String^ calendarI
String^ DateCalculatorViewModel::GetDateDiffString() const
{
String^ result = L"";
wstring result;
bool addDelimiter = false;
AppResourceProvider resourceLoader = AppResourceProvider::GetInstance();
auto yearCount = m_dateDiffResult.year;
if (yearCount > 0)
{
result = String::Concat(GetLocalizedNumberString(yearCount), L" ");
result += GetLocalizedNumberString(yearCount)->Data();
result += L" ";
if (yearCount > 1)
{
result = String::Concat(result, resourceLoader.GetResourceString(L"Date_Years"));
result += resourceLoader.GetResourceString(L"Date_Years")->Data();
}
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
@ -272,22 +273,23 @@ String^ DateCalculatorViewModel::GetDateDiffString() const
{
if (addDelimiter)
{
result = String::Concat(result, m_listSeparator);
result += m_listSeparator;
}
else
{
addDelimiter = true;
}
result = String::Concat(result, String::Concat(GetLocalizedNumberString(monthCount), L" "));
result += GetLocalizedNumberString(monthCount)->Data();
result += L" ";
if (monthCount > 1)
{
result = String::Concat(result, resourceLoader.GetResourceString(L"Date_Months"));
result += resourceLoader.GetResourceString(L"Date_Months")->Data();
}
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)
{
result = String::Concat(result, m_listSeparator);
result += m_listSeparator;
}
else
{
addDelimiter = true;
}
result = String::Concat(result, String::Concat(GetLocalizedNumberString(weekCount), L" "));
result += GetLocalizedNumberString(weekCount)->Data();
result += L" ";
if (weekCount > 1)
{
result = String::Concat(result, resourceLoader.GetResourceString(L"Date_Weeks"));
result += resourceLoader.GetResourceString(L"Date_Weeks")->Data();
}
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)
{
result = String::Concat(result, m_listSeparator);
result += m_listSeparator;
}
else
{
addDelimiter = true;
}
result = String::Concat(result, String::Concat(GetLocalizedNumberString(dayCount), L" "));
result += GetLocalizedNumberString(dayCount)->Data();
result += L" ";
if (dayCount > 1)
{
result = String::Concat(result, resourceLoader.GetResourceString(L"Date_Days"));
result += resourceLoader.GetResourceString(L"Date_Days")->Data();
}
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^ strDateUnit;
wstring result = GetLocalizedNumberString(m_dateDiffResultInDays.day)->Data();
result += L" ";
// Display the result as '1 day' or 'N days'
if (m_dateDiffResultInDays.day > 1)
{
strDateUnit = AppResourceProvider::GetInstance().GetResourceString(L"Date_Days");
result += AppResourceProvider::GetInstance().GetResourceString(L"Date_Days")->Data();
}
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)

View File

@ -146,7 +146,7 @@ namespace CalculatorApp
CalculatorApp::Common::DateCalculation::DateUnit m_daysOutputFormat;
CalculatorApp::Common::DateCalculation::DateUnit m_allDateUnitsOutputFormat;
Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ m_dateTimeFormatter;
Platform::String^ m_listSeparator;
std::wstring m_listSeparator;
};
}
}

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.
#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)
{
// 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);
}
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();