From: Peter Edberg Date: Wed, 10 Apr 2019 19:06:57 +0000 (-0700) Subject: ICU-20550 jaEra: use all valid eras for calendar calculations (#614) X-Git-Tag: release-64-2-rc~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5b3f1c01ed31f50a24c5e57b4b9cd558982e088e;p=icu ICU-20550 jaEra: use all valid eras for calendar calculations (#614) --- diff --git a/icu4c/source/i18n/japancal.cpp b/icu4c/source/i18n/japancal.cpp index b15761c0bfc..1cb43f364a0 100644 --- a/icu4c/source/i18n/japancal.cpp +++ b/icu4c/source/i18n/japancal.cpp @@ -250,7 +250,7 @@ int32_t JapaneseCalendar::handleGetLimit(UCalendarDateFields field, ELimitType l if (limitType == UCAL_LIMIT_MINIMUM || limitType == UCAL_LIMIT_GREATEST_MINIMUM) { return 0; } - return gCurrentEra; + return gJapaneseEraRules->getNumberOfEras() - 1; // max known era, not gCurrentEra case UCAL_YEAR: { switch (limitType) { @@ -282,7 +282,7 @@ int32_t JapaneseCalendar::getActualMaximum(UCalendarDateFields field, UErrorCode if (U_FAILURE(status)) { return 0; // error case... any value } - if (era == gCurrentEra) { + if (era == gJapaneseEraRules->getNumberOfEras() - 1) { // max known era, not gCurrentEra // TODO: Investigate what value should be used here - revisit after 4.0. return handleGetLimit(UCAL_YEAR, UCAL_LIMIT_MAXIMUM); } else { diff --git a/icu4c/source/test/cintltst/ccaltst.c b/icu4c/source/test/cintltst/ccaltst.c index d5860ae73c1..ce84ddd870f 100644 --- a/icu4c/source/test/cintltst/ccaltst.c +++ b/icu4c/source/test/cintltst/ccaltst.c @@ -40,6 +40,7 @@ void TestGetTZTransition(void); void TestGetWindowsTimeZoneID(void); void TestGetTimeZoneIDByWindowsID(void); +void TestJpnCalAddSetNextEra(); void addCalTest(TestNode** root); @@ -62,6 +63,7 @@ void addCalTest(TestNode** root) addTest(root, &TestGetTZTransition, "tsformat/ccaltst/TestGetTZTransition"); addTest(root, &TestGetWindowsTimeZoneID, "tsformat/ccaltst/TestGetWindowsTimeZoneID"); addTest(root, &TestGetTimeZoneIDByWindowsID, "tsformat/ccaltst/TestGetTimeZoneIDByWindowsID"); + addTest(root, &TestJpnCalAddSetNextEra, "tsformat/ccaltst/TestJpnCalAddSetNextEra"); } /* "GMT" */ @@ -2455,5 +2457,51 @@ void TestGetTimeZoneIDByWindowsID() { } } +// The following currently assumes that Reiwa is the last known/valid era. +// Filed ICU-20551 to generalize this when we have more time... +void TestJpnCalAddSetNextEra() { + UErrorCode status = U_ZERO_ERROR; + UCalendar *jCal = ucal_open(NULL, 0, "ja_JP@calendar=japanese", UCAL_DEFAULT, &status); + if ( U_FAILURE(status) ) { + log_data_err("FAIL: ucal_open for ja_JP@calendar=japanese, status %s\n", u_errorName(status)); + } else { + ucal_clear(jCal); // This sets to 1970, in Showa + int32_t sEra = ucal_get(jCal, UCAL_ERA, &status); // Don't assume era number for Showa + if ( U_FAILURE(status) ) { + log_data_err("FAIL: ucal_get ERA for Showa, status %s\n", u_errorName(status)); + } else { + int32_t iEra, eYear; + int32_t startYears[4] = { 1926, 1989, 2019, 0 }; // start years for Showa, Heisei, Reiwa; 0 marks invalid era + for (iEra = 1; iEra < 3; iEra++) { + status = U_ZERO_ERROR; + ucal_clear(jCal); + ucal_set(jCal, UCAL_ERA, sEra+iEra); + eYear = ucal_get(jCal, UCAL_EXTENDED_YEAR, &status); + if ( U_FAILURE(status) ) { + log_err("FAIL: set %d, ucal_get EXTENDED_YEAR, status %s\n", iEra, u_errorName(status)); + } else if (eYear != startYears[iEra]) { + log_err("ERROR: set %d, expected start year %d but get %d\n", iEra, startYears[iEra], eYear); + } else { + ucal_add(jCal, UCAL_ERA, 1, &status); + if ( U_FAILURE(status) ) { + log_err("FAIL: set %d, ucal_add ERA 1, status %s\n", iEra, u_errorName(status)); + } else { + eYear = ucal_get(jCal, UCAL_EXTENDED_YEAR, &status); + if ( U_FAILURE(status) ) { + log_err("FAIL: set %d then add ERA 1, ucal_get EXTENDED_YEAR, status %s\n", iEra, u_errorName(status)); + } else { + // If this is the last valid era, we expect adding an era to pin to the current era + int32_t nextEraStart = (startYears[iEra+1] == 0)? startYears[iEra]: startYears[iEra+1]; + if (eYear != nextEraStart) { + log_err("ERROR: set %d then add ERA 1, expected start year %d but get %d\n", iEra, nextEraStart, eYear); + } + } + } + } + } + } + ucal_close(jCal); + } +} #endif /* #if !UCONFIG_NO_FORMATTING */