Hi all,

This patch fixes the week of month calculation in GregorianCalendar.
Considering this was broken it's possible the stuff that allows you
to set the date with the day of the week and the week of the month
is also broken.  Not to mention the week of the year stuff.  Damn.

Cheers,
Gary
Index: ChangeLog
===================================================================
RCS file: /cvsroot/classpath/classpath/ChangeLog,v
retrieving revision 1.9215
diff -u -r1.9215 ChangeLog
--- ChangeLog   5 Apr 2007 12:41:33 -0000       1.9215
+++ ChangeLog   5 Apr 2007 12:52:32 -0000
@@ -1,3 +1,8 @@
+2007-04-05  Gary Benson  <[EMAIL PROTECTED]>
+
+       * java/util/GregorianCalendar.java
+       (computeFields): Fix WEEK_OF_MONTH calculation.
+
 2007-04-05  Christian Thalinger  <[EMAIL PROTECTED]>
 
        PR classpath/22800:
Index: java/util/GregorianCalendar.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/util/GregorianCalendar.java,v
retrieving revision 1.48
diff -u -r1.48 GregorianCalendar.java
--- java/util/GregorianCalendar.java    4 Apr 2007 15:31:55 -0000       1.48
+++ java/util/GregorianCalendar.java    5 Apr 2007 12:52:32 -0000
@@ -1,5 +1,5 @@
 /* java.util.GregorianCalendar
-   Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004
+   Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004, 2007
    Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
@@ -841,13 +841,24 @@
     // which day of the week are we (0..6), relative to getFirstDayOfWeek
     int relativeWeekday = (7 + fields[DAY_OF_WEEK] - getFirstDayOfWeek()) % 7;
 
-    fields[WEEK_OF_MONTH] = (fields[DAY_OF_MONTH] - relativeWeekday + 12) / 7;
+    // which day of the week is the first of this month?
+    // nb 35 is the smallest multiple of 7 that ensures that
+    // the left hand side of the modulo operator is positive.
+    int relativeWeekdayOfFirst = (relativeWeekday - fields[DAY_OF_MONTH]
+                                 + 1 + 35) % 7;
+
+    // which week of the month is the first of this month in?
+    int minDays = getMinimalDaysInFirstWeek();
+    int weekOfFirst = ((7 - relativeWeekdayOfFirst) >= minDays) ? 1 : 0;
+
+    // which week of the month is this day in?
+    fields[WEEK_OF_MONTH] = (fields[DAY_OF_MONTH]
+                            + relativeWeekdayOfFirst - 1) / 7 + weekOfFirst;
 
     int weekOfYear = (fields[DAY_OF_YEAR] - relativeWeekday + 6) / 7;
 
     // Do the Correction: getMinimalDaysInFirstWeek() is always in the 
     // first week.
-    int minDays = getMinimalDaysInFirstWeek();
     int firstWeekday = (7 + getWeekDay(fields[YEAR], minDays)
                        - getFirstDayOfWeek()) % 7;
     if (minDays - firstWeekday < 1)

Reply via email to