diff --git a/src/CalcViewModel/DateCalculatorViewModel.cpp b/src/CalcViewModel/DateCalculatorViewModel.cpp index d81cd3b..21f5b88 100644 --- a/src/CalcViewModel/DateCalculatorViewModel.cpp +++ b/src/CalcViewModel/DateCalculatorViewModel.cpp @@ -60,8 +60,7 @@ DateCalculatorViewModel::DateCalculatorViewModel() auto today = calendar->GetDateTime(); // FromDate and ToDate should be clipped (adjusted to a consistent hour in UTC) - m_fromDate = ClipTime(today); - m_toDate = ClipTime(today); + m_fromDate = m_toDate = ClipTime(today, true); // StartDate should not be clipped m_startDate = today; @@ -84,7 +83,7 @@ DateCalculatorViewModel::DateCalculatorViewModel() DayOfWeek trueDayOfWeek = calendar->DayOfWeek; - DateTime clippedTime = ClipTime(today); + DateTime clippedTime = ClipTime(today, false); calendar->SetDateTime(clippedTime); if (calendar->DayOfWeek != trueDayOfWeek) { @@ -119,8 +118,8 @@ void DateCalculatorViewModel::OnInputsChanged() if (m_IsDateDiffMode) { - DateTime clippedFromDate = ClipTime(FromDate); - DateTime clippedToDate = ClipTime(ToDate); + DateTime clippedFromDate = ClipTime(FromDate, true); + DateTime clippedToDate = ClipTime(ToDate, true); // Calculate difference between two dates m_dateCalcEngine->GetDateDifference(clippedFromDate, clippedToDate, m_allDateUnitsOutputFormat, &dateDiff); @@ -367,12 +366,34 @@ String ^ DateCalculatorViewModel::GetLocalizedNumberString(int value) const return ref new String(numberStr.c_str()); } -// Adjusts the given DateTime to 12AM (UTC) of the same day -DateTime DateCalculatorViewModel::ClipTime(DateTime dateTime) +/// +/// Adjusts the given DateTime to 12AM of the same day +/// +/// DateTime to clip +/// Adjust the datetime using local time (by default adjust using UTC time) +DateTime DateCalculatorViewModel::ClipTime(DateTime dateTime, bool adjustUsingLocalTime) { + DateTime referenceDateTime; + if (adjustUsingLocalTime) + { + FILETIME fileTime; + fileTime.dwLowDateTime = (DWORD)(dateTime.UniversalTime & 0xffffffff); + fileTime.dwHighDateTime = (DWORD)(dateTime.UniversalTime >> 32); + + FILETIME localFileTime; + FileTimeToLocalFileTime(&fileTime, &localFileTime); + + referenceDateTime.UniversalTime = (DWORD)localFileTime.dwHighDateTime; + referenceDateTime.UniversalTime <<= 32; + referenceDateTime.UniversalTime |= (DWORD)localFileTime.dwLowDateTime; + } + else + { + referenceDateTime = dateTime; + } auto calendar = ref new Calendar(); calendar->ChangeTimeZone("UTC"); - calendar->SetDateTime(dateTime); + calendar->SetDateTime(referenceDateTime); calendar->Period = calendar->FirstPeriodInThisDay; calendar->Hour = calendar->FirstHourInThisPeriod; calendar->Minute = 0; diff --git a/src/CalcViewModel/DateCalculatorViewModel.h b/src/CalcViewModel/DateCalculatorViewModel.h index 9423a10..c1cce24 100644 --- a/src/CalcViewModel/DateCalculatorViewModel.h +++ b/src/CalcViewModel/DateCalculatorViewModel.h @@ -110,7 +110,7 @@ namespace CalculatorApp Platform::String ^ GetDateDiffString() const; Platform::String ^ GetDateDiffStringInDays() const; Platform::String ^ GetLocalizedNumberString(int value) const; - static Windows::Foundation::DateTime ClipTime(Windows::Foundation::DateTime dateTime); + static Windows::Foundation::DateTime ClipTime(Windows::Foundation::DateTime dateTime, bool adjustToLocalTime); property bool IsOutOfBound {