Modify ClipTime to adjust to local time before clipping (#558)

This commit is contained in:
Rudy Huyn 2019-06-26 16:17:47 -07:00 committed by Eric Wong
parent 499e29933c
commit cb5dfdda7b
2 changed files with 30 additions and 9 deletions

View File

@ -60,8 +60,7 @@ DateCalculatorViewModel::DateCalculatorViewModel()
auto today = calendar->GetDateTime(); auto today = calendar->GetDateTime();
// FromDate and ToDate should be clipped (adjusted to a consistent hour in UTC) // FromDate and ToDate should be clipped (adjusted to a consistent hour in UTC)
m_fromDate = ClipTime(today); m_fromDate = m_toDate = ClipTime(today, true);
m_toDate = ClipTime(today);
// StartDate should not be clipped // StartDate should not be clipped
m_startDate = today; m_startDate = today;
@ -84,7 +83,7 @@ DateCalculatorViewModel::DateCalculatorViewModel()
DayOfWeek trueDayOfWeek = calendar->DayOfWeek; DayOfWeek trueDayOfWeek = calendar->DayOfWeek;
DateTime clippedTime = ClipTime(today); DateTime clippedTime = ClipTime(today, false);
calendar->SetDateTime(clippedTime); calendar->SetDateTime(clippedTime);
if (calendar->DayOfWeek != trueDayOfWeek) if (calendar->DayOfWeek != trueDayOfWeek)
{ {
@ -119,8 +118,8 @@ void DateCalculatorViewModel::OnInputsChanged()
if (m_IsDateDiffMode) if (m_IsDateDiffMode)
{ {
DateTime clippedFromDate = ClipTime(FromDate); DateTime clippedFromDate = ClipTime(FromDate, true);
DateTime clippedToDate = ClipTime(ToDate); DateTime clippedToDate = ClipTime(ToDate, true);
// Calculate difference between two dates // Calculate difference between two dates
m_dateCalcEngine->GetDateDifference(clippedFromDate, clippedToDate, m_allDateUnitsOutputFormat, &dateDiff); 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()); return ref new String(numberStr.c_str());
} }
// Adjusts the given DateTime to 12AM (UTC) of the same day /// <summary>
DateTime DateCalculatorViewModel::ClipTime(DateTime dateTime) /// Adjusts the given DateTime to 12AM of the same day
/// </summary>
/// <param name="dateTime">DateTime to clip</param>
/// <param name="adjustUsingLocalTime">Adjust the datetime using local time (by default adjust using UTC time)</param>
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(); auto calendar = ref new Calendar();
calendar->ChangeTimeZone("UTC"); calendar->ChangeTimeZone("UTC");
calendar->SetDateTime(dateTime); calendar->SetDateTime(referenceDateTime);
calendar->Period = calendar->FirstPeriodInThisDay; calendar->Period = calendar->FirstPeriodInThisDay;
calendar->Hour = calendar->FirstHourInThisPeriod; calendar->Hour = calendar->FirstHourInThisPeriod;
calendar->Minute = 0; calendar->Minute = 0;

View File

@ -110,7 +110,7 @@ namespace CalculatorApp
Platform::String ^ GetDateDiffString() const; Platform::String ^ GetDateDiffString() const;
Platform::String ^ GetDateDiffStringInDays() const; Platform::String ^ GetDateDiffStringInDays() const;
Platform::String ^ GetLocalizedNumberString(int value) 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 property bool IsOutOfBound
{ {