Merge pull request #13 from Microsoft/sanderl/JapaneseEra
Date Calculation: Updated AdjustCalendarDate() to ensure we always add 365 when adding 1 year for Japanese calendar
This commit is contained in:
commit
cb8775926a
@ -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"
|
||||||
@ -273,19 +273,33 @@ bool DateCalculationEngine::TryGetCalendarDaysInYear(_In_ DateTime date, _Out_ U
|
|||||||
// Adds/Subtracts certain value for a particular date unit
|
// Adds/Subtracts certain value for a particular date unit
|
||||||
DateTime DateCalculationEngine::AdjustCalendarDate(Windows::Foundation::DateTime date, DateUnit dateUnit, int difference)
|
DateTime DateCalculationEngine::AdjustCalendarDate(Windows::Foundation::DateTime date, DateUnit dateUnit, int difference)
|
||||||
{
|
{
|
||||||
|
|
||||||
m_calendar->SetDateTime(date);
|
m_calendar->SetDateTime(date);
|
||||||
|
|
||||||
switch (dateUnit)
|
switch (dateUnit)
|
||||||
{
|
{
|
||||||
case DateUnit::Year:
|
case DateUnit::Year:
|
||||||
m_calendar->AddYears(difference);
|
{
|
||||||
break;
|
// In the Japanese calendar, transition years have 2 partial years.
|
||||||
case DateUnit::Month:
|
// It is not guaranteed that adding 1 year will always add 365 days in the Japanese Calendar.
|
||||||
m_calendar->AddMonths(difference);
|
// To work around this quirk, we will change the calendar system to Gregorian before adding 1 year in the Japanese Calendar case only.
|
||||||
break;
|
// We will then return the calendar system back to the Japanese Calendar.
|
||||||
case DateUnit::Week:
|
auto currentCalendarSystem = m_calendar->GetCalendarSystem();
|
||||||
m_calendar->AddWeeks(difference);
|
if (currentCalendarSystem == CalendarIdentifiers::Japanese)
|
||||||
break;
|
{
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
return m_calendar->GetDateTime();
|
return m_calendar->GetDateTime();
|
||||||
|
Loading…
Reference in New Issue
Block a user