From ea8c2f2eb844a744b0368c4fef738d6559915bdb Mon Sep 17 00:00:00 2001 From: Gary Benson Date: Thu, 5 Apr 2007 12:52:44 +0000 Subject: 2007-04-05 Gary Benson * java/util/GregorianCalendar.java (computeFields): Fix WEEK_OF_MONTH calculation. --- ChangeLog | 5 +++++ java/util/GregorianCalendar.java | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index d91d3fb65..3f92a5ea3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2007-04-05 Gary Benson + + * java/util/GregorianCalendar.java + (computeFields): Fix WEEK_OF_MONTH calculation. + 2007-04-05 Christian Thalinger PR classpath/22800: diff --git a/java/util/GregorianCalendar.java b/java/util/GregorianCalendar.java index eb64f1c3f..6eb7ce84e 100644 --- a/java/util/GregorianCalendar.java +++ b/java/util/GregorianCalendar.java @@ -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 @@ public class GregorianCalendar extends Calendar // 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) -- cgit v1.2.1