From f8029942d414726643846091177d7ff5b193f507 Mon Sep 17 00:00:00 2001 From: Stephanie Anderl <46726333+sanderl@users.noreply.github.com> Date: Fri, 8 Feb 2019 12:30:16 -0800 Subject: [PATCH 1/3] Updated the AdjustCalendarDate() to account for the transition year quirk in the Japanese calendar. --- src/CalcViewModel/Common/DateCalculator.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/CalcViewModel/Common/DateCalculator.cpp b/src/CalcViewModel/Common/DateCalculator.cpp index 6b7eba88..22cd4ff5 100644 --- a/src/CalcViewModel/Common/DateCalculator.cpp +++ b/src/CalcViewModel/Common/DateCalculator.cpp @@ -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" @@ -273,12 +273,24 @@ bool DateCalculationEngine::TryGetCalendarDaysInYear(_In_ DateTime date, _Out_ U // Adds/Subtracts certain value for a particular date unit DateTime DateCalculationEngine::AdjustCalendarDate(Windows::Foundation::DateTime date, DateUnit dateUnit, int difference) { + auto currentCalendarSystem = m_calendar->GetCalendarSystem(); + m_calendar->SetDateTime(date); switch (dateUnit) { case DateUnit::Year: + // In the Japanese calendar, transition years have 2 partial years. + // It is not guaranteed that adding 1 year will always add 365 days in the Japanese Calendar. + // To work around this quirk, we will change the calendar system to Gregorian before adding 1 year in the Japanese Calendar case only. + // We will then return the calendar system back to the Japanese Calendar. + if (currentCalendarSystem == L"JapaneseCalendar") + { + m_calendar->ChangeCalendarSystem(L"GregorianCalendar"); + } + m_calendar->AddYears(difference); + m_calendar->ChangeCalendarSystem(currentCalendarSystem); break; case DateUnit::Month: m_calendar->AddMonths(difference); From 1bd4f1870c120d758662219ee6f170360b2fbcd0 Mon Sep 17 00:00:00 2001 From: Stephanie Anderl <46726333+sanderl@users.noreply.github.com> Date: Fri, 8 Feb 2019 15:33:00 -0800 Subject: [PATCH 2/3] Updated calendar strings to use the CalendarIdentifiers object --- src/CalcViewModel/Common/DateCalculator.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CalcViewModel/Common/DateCalculator.cpp b/src/CalcViewModel/Common/DateCalculator.cpp index 22cd4ff5..3e1ce9b3 100644 --- a/src/CalcViewModel/Common/DateCalculator.cpp +++ b/src/CalcViewModel/Common/DateCalculator.cpp @@ -284,9 +284,9 @@ DateTime DateCalculationEngine::AdjustCalendarDate(Windows::Foundation::DateTime // It is not guaranteed that adding 1 year will always add 365 days in the Japanese Calendar. // To work around this quirk, we will change the calendar system to Gregorian before adding 1 year in the Japanese Calendar case only. // We will then return the calendar system back to the Japanese Calendar. - if (currentCalendarSystem == L"JapaneseCalendar") + if (currentCalendarSystem == CalendarIdentifiers::Japanese) { - m_calendar->ChangeCalendarSystem(L"GregorianCalendar"); + m_calendar->ChangeCalendarSystem(CalendarIdentifiers::Gregorian); } m_calendar->AddYears(difference); From 2fc8196104ff45dfaf5cd3b04c545a77d69fae5f Mon Sep 17 00:00:00 2001 From: Stephanie Anderl <46726333+sanderl@users.noreply.github.com> Date: Fri, 8 Feb 2019 15:42:28 -0800 Subject: [PATCH 3/3] Moved all the Japanese Era logic inside the Year case of the switch statement --- src/CalcViewModel/Common/DateCalculator.cpp | 38 +++++++++++---------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/CalcViewModel/Common/DateCalculator.cpp b/src/CalcViewModel/Common/DateCalculator.cpp index 3e1ce9b3..ac0dbbe9 100644 --- a/src/CalcViewModel/Common/DateCalculator.cpp +++ b/src/CalcViewModel/Common/DateCalculator.cpp @@ -273,31 +273,33 @@ bool DateCalculationEngine::TryGetCalendarDaysInYear(_In_ DateTime date, _Out_ U // Adds/Subtracts certain value for a particular date unit DateTime DateCalculationEngine::AdjustCalendarDate(Windows::Foundation::DateTime date, DateUnit dateUnit, int difference) { - auto currentCalendarSystem = m_calendar->GetCalendarSystem(); m_calendar->SetDateTime(date); switch (dateUnit) { - case DateUnit::Year: - // In the Japanese calendar, transition years have 2 partial years. - // It is not guaranteed that adding 1 year will always add 365 days in the Japanese Calendar. - // To work around this quirk, we will change the calendar system to Gregorian before adding 1 year in the Japanese Calendar case only. - // We will then return the calendar system back to the Japanese Calendar. - if (currentCalendarSystem == CalendarIdentifiers::Japanese) + case DateUnit::Year: { - m_calendar->ChangeCalendarSystem(CalendarIdentifiers::Gregorian); - } + // In the Japanese calendar, transition years have 2 partial years. + // It is not guaranteed that adding 1 year will always add 365 days in the Japanese Calendar. + // To work around this quirk, we will change the calendar system to Gregorian before adding 1 year in the Japanese Calendar case only. + // We will then return the calendar system back to the Japanese Calendar. + auto currentCalendarSystem = m_calendar->GetCalendarSystem(); + if (currentCalendarSystem == CalendarIdentifiers::Japanese) + { + m_calendar->ChangeCalendarSystem(CalendarIdentifiers::Gregorian); + } - m_calendar->AddYears(difference); - m_calendar->ChangeCalendarSystem(currentCalendarSystem); - break; - case DateUnit::Month: - m_calendar->AddMonths(difference); - break; - case DateUnit::Week: - m_calendar->AddWeeks(difference); - break; + m_calendar->AddYears(difference); + m_calendar->ChangeCalendarSystem(currentCalendarSystem); + break; + } + case DateUnit::Month: + m_calendar->AddMonths(difference); + break; + case DateUnit::Week: + m_calendar->AddWeeks(difference); + break; } return m_calendar->GetDateTime();