From c603bebfc228ff0383368f7cef550da067e96e8a Mon Sep 17 00:00:00 2001 From: Alan Liu Date: Mon, 17 Mar 2003 17:14:48 +0000 Subject: [PATCH] ICU-1085 make first day of week and minimal days of first week invalidate fields; check valid range X-SVN-Rev: 11335 --- .../dev/test/calendar/CompatibilityTest.java | 6 +- .../dev/test/calendar/IBMCalendarTest.java | 73 ++++++++++++++++++- icu4j/src/com/ibm/icu/util/Calendar.java | 20 ++++- 3 files changed, 90 insertions(+), 9 deletions(-) diff --git a/icu4j/src/com/ibm/icu/dev/test/calendar/CompatibilityTest.java b/icu4j/src/com/ibm/icu/dev/test/calendar/CompatibilityTest.java index adb5a1dca97..1ce978af434 100755 --- a/icu4j/src/com/ibm/icu/dev/test/calendar/CompatibilityTest.java +++ b/icu4j/src/com/ibm/icu/dev/test/calendar/CompatibilityTest.java @@ -5,8 +5,8 @@ ******************************************************************************* * * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/calendar/CompatibilityTest.java,v $ - * $Date: 2002/08/13 21:53:38 $ - * $Revision: 1.8 $ + * $Date: 2003/03/17 17:14:48 $ + * $Revision: 1.9 $ * ******************************************************************************* */ @@ -295,7 +295,7 @@ public class CompatibilityTest extends com.ibm.icu.dev.test.TestFmwk { if (cal.getFirstDayOfWeek() != i) errln("FAIL: set/getFirstDayOfWeek failed"); } - for (i=0; i<=7; ++i) { + for (i=1; i<=7; ++i) { cal.setMinimalDaysInFirstWeek(i); if (cal.getMinimalDaysInFirstWeek() != i) errln("FAIL: set/getFirstDayOfWeek failed"); } diff --git a/icu4j/src/com/ibm/icu/dev/test/calendar/IBMCalendarTest.java b/icu4j/src/com/ibm/icu/dev/test/calendar/IBMCalendarTest.java index 2e754f23a5b..ac0a877d87f 100755 --- a/icu4j/src/com/ibm/icu/dev/test/calendar/IBMCalendarTest.java +++ b/icu4j/src/com/ibm/icu/dev/test/calendar/IBMCalendarTest.java @@ -4,8 +4,8 @@ * others. All Rights Reserved. ******************************************************************************* * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/calendar/IBMCalendarTest.java,v $ - * $Date: 2003/02/18 21:11:22 $ - * $Revision: 1.13 $ + * $Date: 2003/03/17 17:14:48 $ + * $Revision: 1.14 $ ******************************************************************************* */ package com.ibm.icu.dev.test.calendar; @@ -342,6 +342,75 @@ public class IBMCalendarTest extends CalendarTest { Calendar cal = Calendar.getInstance(loc); } + /** + * setFirstDayOfWeek and setMinimalDaysInFirstWeek may change the + * field <=> time mapping, since they affect the interpretation of + * the WEEK_OF_MONTH or WEEK_OF_YEAR fields. + */ + public void TestWeekShift() { + Calendar cal = new GregorianCalendar( + TimeZone.getTimeZone("America/Los_Angelese"), + new Locale("en", "US")); + cal.setTime(new Date(997257600000L)); // Wed Aug 08 01:00:00 PDT 2001 + // In pass one, change the first day of week so that the weeks + // shift in August 2001. In pass two, change the minimal days + // in the first week so that the weeks shift in August 2001. + // August 2001 + // Su Mo Tu We Th Fr Sa + // 1 2 3 4 + // 5 6 7 8 9 10 11 + // 12 13 14 15 16 17 18 + // 19 20 21 22 23 24 25 + // 26 27 28 29 30 31 + for (int pass=0; pass<2; ++pass) { + if (pass==0) { + cal.setFirstDayOfWeek(Calendar.WEDNESDAY); + cal.setMinimalDaysInFirstWeek(4); + } else { + cal.setFirstDayOfWeek(Calendar.SUNDAY); + cal.setMinimalDaysInFirstWeek(4); + } + cal.add(Calendar.DATE, 1); // Force recalc + cal.add(Calendar.DATE, -1); + + Date time1 = cal.getTime(); // Get time -- should not change + + // Now change a week parameter and then force a recalc. + // The bug is that the recalc should not be necessary -- + // calendar should do so automatically. + if (pass==0) { + cal.setFirstDayOfWeek(Calendar.THURSDAY); + } else { + cal.setMinimalDaysInFirstWeek(5); + } + + int woy1 = cal.get(Calendar.WEEK_OF_YEAR); + int wom1 = cal.get(Calendar.WEEK_OF_MONTH); + + cal.add(Calendar.DATE, 1); // Force recalc + cal.add(Calendar.DATE, -1); + + int woy2 = cal.get(Calendar.WEEK_OF_YEAR); + int wom2 = cal.get(Calendar.WEEK_OF_MONTH); + + Date time2 = cal.getTime(); + + if (!time1.equals(time2)) { + errln("FAIL: shifting week should not alter time"); + } else { + logln(time1.toString()); + } + if (woy1 == woy2 && wom1 == wom2) { + logln("Ok: WEEK_OF_YEAR: " + woy1 + + ", WEEK_OF_MONTH: " + wom1); + } else { + errln("FAIL: WEEK_OF_YEAR: " + woy1 + " => " + woy2 + + ", WEEK_OF_MONTH: " + wom1 + " => " + wom2 + + " after week shift"); + } + } + } + /** * Miscellaneous tests to increase coverage. */ diff --git a/icu4j/src/com/ibm/icu/util/Calendar.java b/icu4j/src/com/ibm/icu/util/Calendar.java index 3c643e82525..4d1080f0b17 100755 --- a/icu4j/src/com/ibm/icu/util/Calendar.java +++ b/icu4j/src/com/ibm/icu/util/Calendar.java @@ -3224,7 +3224,13 @@ public abstract class Calendar implements Serializable, Cloneable { */ public void setFirstDayOfWeek(int value) { - firstDayOfWeek = value; + if (firstDayOfWeek != value) { + if (value < SUNDAY || value > SATURDAY) { + throw new IllegalArgumentException("Invalid day of week"); + } + firstDayOfWeek = value; + areFieldsSet = false; + } } /** @@ -3249,7 +3255,13 @@ public abstract class Calendar implements Serializable, Cloneable { */ public void setMinimalDaysInFirstWeek(int value) { - minimalDaysInFirstWeek = value; + if (minimalDaysInFirstWeek != value) { + if (value < 1 || value > 7) { + throw new IllegalArgumentException("Invalid days in first week"); + } + minimalDaysInFirstWeek = value; + areFieldsSet = false; + } } /** @@ -3655,8 +3667,8 @@ public abstract class Calendar implements Serializable, Cloneable { /* cache update */ cachedLocaleData.put(desiredLocale, data); } - firstDayOfWeek = data[0]; - minimalDaysInFirstWeek = data[1]; + setFirstDayOfWeek(data[0]); + setMinimalDaysInFirstWeek(data[1]); } /**