]> granicus.if.org Git - icu/commitdiff
ICU-13548 Fixed a calendar calculation problem with setting week-of-year and year.
authorYoshito Umaoka <y.umaoka@gmail.com>
Wed, 21 Feb 2018 21:09:33 +0000 (21:09 +0000)
committerYoshito Umaoka <y.umaoka@gmail.com>
Wed, 21 Feb 2018 21:09:33 +0000 (21:09 +0000)
X-SVN-Rev: 40966

icu4c/source/i18n/calendar.cpp
icu4c/source/test/intltest/calregts.cpp
icu4c/source/test/intltest/calregts.h
icu4j/main/classes/core/src/com/ibm/icu/util/Calendar.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/calendar/CalendarRegressionTest.java

index 092dc4c1d9e558709166bad139ed8c9213a2030d..526a5a70b4ab957d65a89bdb158573d6702892b1 100644 (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
index 24951e5b8aa9f829710883b20a63a7d0a1b5bb99..da522637c6f9e83c30dc8ca20a4d44d4f774b48f 100644 (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 */
index 7d36fab0b4546032a81745a92cb816b8c376899d..b4166a0c61d3f72a09a0c3afdf9cf7def75be523 100644 (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) ;
index 6529867e708d9ef8cc8d2ea93d725be97d02e5ef..81d35403ac5c8420363056e5e3e798ca17ddcd96 100644 (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();
         }
index 4371b6720396717518886d6d9028e159094fa34e..8f73d16217803b4b7657d6d9451e76159b2c60b6 100644 (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