ICU-13548 Fixed a calendar calculation problem with setting week-of-year and year.

X-SVN-Rev: 40966
This commit is contained in:
Yoshito Umaoka 2018-02-21 21:09:33 +00:00
parent 57a43a8cf6
commit 44b2617d44
5 changed files with 39 additions and 6 deletions

View File

@ -3223,14 +3223,14 @@ int32_t Calendar::handleComputeJulianDay(UCalendarDateFields bestField) {
bestField == UCAL_DAY_OF_WEEK_IN_MONTH);
int32_t year;
if (bestField == UCAL_WEEK_OF_YEAR) {
year = internalGet(UCAL_YEAR_WOY, handleGetExtendedYear());
internalSet(UCAL_EXTENDED_YEAR, year);
if (bestField == UCAL_WEEK_OF_YEAR && newerField(UCAL_YEAR_WOY, UCAL_YEAR) == UCAL_YEAR_WOY) {
year = internalGet(UCAL_YEAR_WOY);
} else {
year = handleGetExtendedYear();
internalSet(UCAL_EXTENDED_YEAR, year);
}
internalSet(UCAL_EXTENDED_YEAR, year);
#if defined (U_DEBUG_CAL)
fprintf(stderr, "%s:%d: bestField= %s - y=%d\n", __FILE__, __LINE__, fldName(bestField), year);
#endif

View File

@ -17,6 +17,7 @@
#include "unicode/simpletz.h"
#include "unicode/smpdtfmt.h"
#include "unicode/strenum.h"
#include "unicode/localpointer.h"
#include "cmemory.h"
#include "caltest.h"
#include "unicode/localpointer.h"
@ -94,6 +95,7 @@ CalendarRegressionTest::runIndexedTest( int32_t index, UBool exec, const char* &
CASE(51,TestT11632);
CASE(52,TestPersianCalOverflow);
CASE(53,TestIslamicCalOverflow);
CASE(54,TestWeekOfYear13548);
default: name = ""; break;
}
}
@ -3051,4 +3053,20 @@ void CalendarRegressionTest::TestIslamicCalOverflow(void) {
}
}
void CalendarRegressionTest::TestWeekOfYear13548(void) {
int32_t year = 2000;
UErrorCode status = U_ZERO_ERROR;
LocalPointer<Calendar> cal(Calendar::createInstance(status));
failure(status, "Calendar::createInstance(status)");
cal->set(UCAL_YEAR, year);
cal->set(UCAL_WEEK_OF_YEAR, 4);
int32_t resultYear = cal->get(UCAL_YEAR, status);
failure(status, "get(UCAL_YEAR, status)");
if (year != resultYear) {
errln((UnicodeString)"Fail: Expected year=" + year + ", actual=" + resultYear);
}
}
#endif /* #if !UCONFIG_NO_FORMATTING */

View File

@ -80,6 +80,7 @@ public:
void TestT11632(void);
void TestPersianCalOverflow(void);
void TestIslamicCalOverflow(void);
void TestWeekOfYear13548(void);
void printdate(GregorianCalendar *cal, const char *string);
void dowTest(UBool lenient) ;

View File

@ -5959,12 +5959,12 @@ public abstract class Calendar implements Serializable, Cloneable, Comparable<Ca
int year;
if (bestField == WEEK_OF_YEAR) {
if (bestField == WEEK_OF_YEAR && newerField(YEAR_WOY, YEAR) == YEAR_WOY) {
// Nota Bene! It is critical that YEAR_WOY be used as the year here, if it is
// set. Otherwise, when WOY is the best field, the year may be wrong at the
// extreme limits of the year. If YEAR_WOY is not set then it will fall back.
// TODO: Should resolveField(YEAR_PRECEDENCE) be brought to bear?
year = internalGet(YEAR_WOY, handleGetExtendedYear());
year = internalGet(YEAR_WOY);
} else {
year = handleGetExtendedYear();
}

View File

@ -2541,5 +2541,19 @@ public class CalendarRegressionTest extends com.ibm.icu.dev.test.TestFmwk {
}
}
}
@Test
public void TestWeekOfYear13548() {
int year = 2000;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.WEEK_OF_YEAR, 4);
int resultYear = cal.get(Calendar.YEAR);
if (year != resultYear) {
errln("Fail: Expected year=" + year + ", actual=" + resultYear);
}
}
}
//eof