You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by "Chen Shunfei (JIRA)" <ji...@apache.org> on 2007/10/09 04:29:50 UTC

[jira] Created: (HARMONY-4907) [classlib][luni] Calendar.get(Calendar.WEEK_OF_MONTH)

[classlib][luni] Calendar.get(Calendar.WEEK_OF_MONTH)
-----------------------------------------------------

                 Key: HARMONY-4907
                 URL: https://issues.apache.org/jira/browse/HARMONY-4907
             Project: Harmony
          Issue Type: Bug
          Components: Classlib
         Environment: harmony-jre-579330
WindowXP
            Reporter: Chen Shunfei


Here is the testcase:
import java.util.Calendar;
import java.util.GregorianCalendar;
import junit.framework.TestCase;

public class Test extends TestCase {
	public void testDayOfWeekInMonth() {
		GregorianCalendar c = new GregorianCalendar();
		int minimalDaysInFirstWeek = 2;
		int firstDayOfWeek = Calendar.SUNDAY;
		c.setFirstDayOfWeek(firstDayOfWeek);
		int month = Calendar.DECEMBER;
		int year = 2007;
		int day = 1;
		c.set(year, month, day);
		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
		int relativeDayOfWeek = (7 + dayOfWeek - firstDayOfWeek) % 7;
		int relativeDayOfFirst = (relativeDayOfWeek - day + 1 + 35) % 7;
		int weekOfFirst = ((7 - relativeDayOfFirst) >= minimalDaysInFirstWeek) ? 1
				: 0;
		int weekOfMonth = (day + relativeDayOfFirst - 1) / 7 + weekOfFirst;
		assertEquals(c.get(Calendar.WEEK_OF_MONTH), weekOfMonth);		
	}
}


-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (HARMONY-4907) [classlib][luni] Calendar.get(Calendar.WEEK_OF_MONTH) returns wrong value

Posted by "Ramana Polavarapu (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/HARMONY-4907?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Ramana Polavarapu updated HARMONY-4907:
---------------------------------------

    Attachment: GregorianCalendar.patch

Attaching the patch for the problem

> [classlib][luni] Calendar.get(Calendar.WEEK_OF_MONTH) returns wrong value
> -------------------------------------------------------------------------
>
>                 Key: HARMONY-4907
>                 URL: https://issues.apache.org/jira/browse/HARMONY-4907
>             Project: Harmony
>          Issue Type: Bug
>          Components: Classlib
>         Environment: harmony-jre-579330
> WindowXP
>            Reporter: Chen Shunfei
>            Assignee: Tony Wu
>            Priority: Trivial
>         Attachments: GregorianCalendar.patch
>
>
> Here is the testcase:
> import java.util.Calendar;
> import java.util.GregorianCalendar;
> import junit.framework.TestCase;
> public class Test extends TestCase {
> 	public void testDayOfWeekInMonth() {
> 		GregorianCalendar c = new GregorianCalendar();
> 		int minimalDaysInFirstWeek = 2;
> 		int firstDayOfWeek = Calendar.SUNDAY;
> 		c.setFirstDayOfWeek(firstDayOfWeek);
> 		int month = Calendar.DECEMBER;
> 		int year = 2007;
> 		int day = 1;
> 		c.set(year, month, day);
> 		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
> 		int relativeDayOfWeek = (7 + dayOfWeek - firstDayOfWeek) % 7;
> 		int relativeDayOfFirst = (relativeDayOfWeek - day + 1 + 35) % 7;
> 		int weekOfFirst = ((7 - relativeDayOfFirst) >= minimalDaysInFirstWeek) ? 1
> 				: 0;
> 		int weekOfMonth = (day + relativeDayOfFirst - 1) / 7 + weekOfFirst;
> 		assertEquals(c.get(Calendar.WEEK_OF_MONTH), weekOfMonth);		
> 	}
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (HARMONY-4907) [classlib][luni] Calendar.get(Calendar.WEEK_OF_MONTH) returns wrong value

Posted by "Ramana Polavarapu (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/HARMONY-4907?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12773827#action_12773827 ] 

Ramana Polavarapu commented on HARMONY-4907:
--------------------------------------------

Hi Tony:

I looked into this bug.  The problem is with GregorianCalendar.fullFieldCalk method.  Specifically, the culprit is line 551:
fields[WEEK_OF_MONTH] = (date - 1 + mod7(days - date - 2
                - (getFirstDayOfWeek() - 1))) / 7 + 1;
        This computation does not take into consideration minimalDaysInFirstWeek.  We have to modify above lines with the following:
        fields[WEEK_OF_MONTH] = computeWeekOfMonth();
To compute week of month, any one of the following two methods can be used.  I wrote the first one.  Later, I extracted from your test case the second method.  There are two other points that I would like to make:
1. In your test case, you need to add the following line:
c.setMinimalDaysInFirstWeek(minimalDaysInFirstWeek);
2. When this variable is not set, RI returns 1 but Harmony returns 4.  Do we need to match the behavior of RI? 

	/**
	 * The algorithm first figures out the beginning of the next week.
	 * The for loop in the method accomplishes the above task.
	 * Then, it computes the first occurrence
	 * of the first day of the week in the given month.
	 * Before the first occurrence if there are more days than
	 * the minimum required days for first week,
	 * those days belong to week 1.
	 * Otherwise, they belong to week 0.
	 * This method should not be called before 
	 * <code>filds[DATE] is set.
	 * @return week of the month.  The value will be 0 if 
	 * minimum required days for a week are more than the days before 
	 * the first "first day of week" at the beginning of the month.
	 */
    private int computeWeekOfMonth() {
    	int firstDay = getFirstDayOfWeek();
    	int tempDate = fields[DATE];
    	int tempDayOfWeek = DAY_OF_WEEK;
    	for(int i = 0; i < 7; i++)
    		if(firstDay == tempDayOfWeek)
    			break;
    		else {
    			tempDate++;
    			tempDayOfWeek++;
    		}
    	int tempFirstWeek = tempDate%7 >= getMinimalDaysInFirstWeek() ? 1 : 0;
    	return fields[DATE] / 7 + tempFirstWeek;
    }

    /**
     * This method does the same as above.
     * This method more compact.
     * Consequently, following logic is a bit more difficult.
     * 
     * @return
     */
    private int computeWeekOfMonthTony() {
		int dayOfWeek = fields[DAY_OF_WEEK];
		int relativeDayOfWeek = (7 + dayOfWeek - getFirstDayOfWeek()) % 7;
		int relativeDayOfFirst = (relativeDayOfWeek - fields[DATE] + 1 + 35) % 7;
		int weekOfFirst = ((7 - relativeDayOfFirst) >= getMinimalDaysInFirstWeek()) ? 1 : 0;
		return (fields[DATE] + relativeDayOfFirst - 1) / 7 + weekOfFirst;
	}

Regards,

Ramana
	


> [classlib][luni] Calendar.get(Calendar.WEEK_OF_MONTH) returns wrong value
> -------------------------------------------------------------------------
>
>                 Key: HARMONY-4907
>                 URL: https://issues.apache.org/jira/browse/HARMONY-4907
>             Project: Harmony
>          Issue Type: Bug
>          Components: Classlib
>         Environment: harmony-jre-579330
> WindowXP
>            Reporter: Chen Shunfei
>            Assignee: Tony Wu
>            Priority: Trivial
>
> Here is the testcase:
> import java.util.Calendar;
> import java.util.GregorianCalendar;
> import junit.framework.TestCase;
> public class Test extends TestCase {
> 	public void testDayOfWeekInMonth() {
> 		GregorianCalendar c = new GregorianCalendar();
> 		int minimalDaysInFirstWeek = 2;
> 		int firstDayOfWeek = Calendar.SUNDAY;
> 		c.setFirstDayOfWeek(firstDayOfWeek);
> 		int month = Calendar.DECEMBER;
> 		int year = 2007;
> 		int day = 1;
> 		c.set(year, month, day);
> 		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
> 		int relativeDayOfWeek = (7 + dayOfWeek - firstDayOfWeek) % 7;
> 		int relativeDayOfFirst = (relativeDayOfWeek - day + 1 + 35) % 7;
> 		int weekOfFirst = ((7 - relativeDayOfFirst) >= minimalDaysInFirstWeek) ? 1
> 				: 0;
> 		int weekOfMonth = (day + relativeDayOfFirst - 1) / 7 + weekOfFirst;
> 		assertEquals(c.get(Calendar.WEEK_OF_MONTH), weekOfMonth);		
> 	}
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Assigned: (HARMONY-4907) [classlib][luni] Calendar.get(Calendar.WEEK_OF_MONTH) returns wrong value

Posted by "Tony Wu (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/HARMONY-4907?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Tony Wu reassigned HARMONY-4907:
--------------------------------

    Assignee: Tony Wu

> [classlib][luni] Calendar.get(Calendar.WEEK_OF_MONTH) returns wrong value
> -------------------------------------------------------------------------
>
>                 Key: HARMONY-4907
>                 URL: https://issues.apache.org/jira/browse/HARMONY-4907
>             Project: Harmony
>          Issue Type: Bug
>          Components: Classlib
>         Environment: harmony-jre-579330
> WindowXP
>            Reporter: Chen Shunfei
>            Assignee: Tony Wu
>
> Here is the testcase:
> import java.util.Calendar;
> import java.util.GregorianCalendar;
> import junit.framework.TestCase;
> public class Test extends TestCase {
> 	public void testDayOfWeekInMonth() {
> 		GregorianCalendar c = new GregorianCalendar();
> 		int minimalDaysInFirstWeek = 2;
> 		int firstDayOfWeek = Calendar.SUNDAY;
> 		c.setFirstDayOfWeek(firstDayOfWeek);
> 		int month = Calendar.DECEMBER;
> 		int year = 2007;
> 		int day = 1;
> 		c.set(year, month, day);
> 		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
> 		int relativeDayOfWeek = (7 + dayOfWeek - firstDayOfWeek) % 7;
> 		int relativeDayOfFirst = (relativeDayOfWeek - day + 1 + 35) % 7;
> 		int weekOfFirst = ((7 - relativeDayOfFirst) >= minimalDaysInFirstWeek) ? 1
> 				: 0;
> 		int weekOfMonth = (day + relativeDayOfFirst - 1) / 7 + weekOfFirst;
> 		assertEquals(c.get(Calendar.WEEK_OF_MONTH), weekOfMonth);		
> 	}
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (HARMONY-4907) [classlib][luni] Calendar.get(Calendar.WEEK_OF_MONTH) returns wrong value

Posted by "Oliver Deakin (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/HARMONY-4907?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12848804#action_12848804 ] 

Oliver Deakin commented on HARMONY-4907:
----------------------------------------

Hi Ramana - applying this patch causes one failure for me in CalendarTest.test_clear():

junit.framework.AssertionFailedError: Field 5 Should equal to 20. expected:<20> but was:<13>
at org.apache.harmony.luni.tests.java.util.CalendarTest.test_clear(CalendarTest.java:605)
at java.lang.reflect.VMReflection.invokeMethod(VMReflection.java)

> [classlib][luni] Calendar.get(Calendar.WEEK_OF_MONTH) returns wrong value
> -------------------------------------------------------------------------
>
>                 Key: HARMONY-4907
>                 URL: https://issues.apache.org/jira/browse/HARMONY-4907
>             Project: Harmony
>          Issue Type: Bug
>          Components: Classlib
>         Environment: harmony-jre-579330
> WindowXP
>            Reporter: Chen Shunfei
>            Assignee: Tony Wu
>            Priority: Trivial
>         Attachments: GregorianCalendar.patch
>
>
> Here is the testcase:
> import java.util.Calendar;
> import java.util.GregorianCalendar;
> import junit.framework.TestCase;
> public class Test extends TestCase {
> 	public void testDayOfWeekInMonth() {
> 		GregorianCalendar c = new GregorianCalendar();
> 		int minimalDaysInFirstWeek = 2;
> 		int firstDayOfWeek = Calendar.SUNDAY;
> 		c.setFirstDayOfWeek(firstDayOfWeek);
> 		int month = Calendar.DECEMBER;
> 		int year = 2007;
> 		int day = 1;
> 		c.set(year, month, day);
> 		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
> 		int relativeDayOfWeek = (7 + dayOfWeek - firstDayOfWeek) % 7;
> 		int relativeDayOfFirst = (relativeDayOfWeek - day + 1 + 35) % 7;
> 		int weekOfFirst = ((7 - relativeDayOfFirst) >= minimalDaysInFirstWeek) ? 1
> 				: 0;
> 		int weekOfMonth = (day + relativeDayOfFirst - 1) / 7 + weekOfFirst;
> 		assertEquals(c.get(Calendar.WEEK_OF_MONTH), weekOfMonth);		
> 	}
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (HARMONY-4907) [classlib][luni] Calendar.get(Calendar.WEEK_OF_MONTH) returns wrong value

Posted by "Tony Wu (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/HARMONY-4907?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Tony Wu updated HARMONY-4907:
-----------------------------

    Priority: Trivial  (was: Major)

> [classlib][luni] Calendar.get(Calendar.WEEK_OF_MONTH) returns wrong value
> -------------------------------------------------------------------------
>
>                 Key: HARMONY-4907
>                 URL: https://issues.apache.org/jira/browse/HARMONY-4907
>             Project: Harmony
>          Issue Type: Bug
>          Components: Classlib
>         Environment: harmony-jre-579330
> WindowXP
>            Reporter: Chen Shunfei
>            Assignee: Tony Wu
>            Priority: Trivial
>
> Here is the testcase:
> import java.util.Calendar;
> import java.util.GregorianCalendar;
> import junit.framework.TestCase;
> public class Test extends TestCase {
> 	public void testDayOfWeekInMonth() {
> 		GregorianCalendar c = new GregorianCalendar();
> 		int minimalDaysInFirstWeek = 2;
> 		int firstDayOfWeek = Calendar.SUNDAY;
> 		c.setFirstDayOfWeek(firstDayOfWeek);
> 		int month = Calendar.DECEMBER;
> 		int year = 2007;
> 		int day = 1;
> 		c.set(year, month, day);
> 		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
> 		int relativeDayOfWeek = (7 + dayOfWeek - firstDayOfWeek) % 7;
> 		int relativeDayOfFirst = (relativeDayOfWeek - day + 1 + 35) % 7;
> 		int weekOfFirst = ((7 - relativeDayOfFirst) >= minimalDaysInFirstWeek) ? 1
> 				: 0;
> 		int weekOfMonth = (day + relativeDayOfFirst - 1) / 7 + weekOfFirst;
> 		assertEquals(c.get(Calendar.WEEK_OF_MONTH), weekOfMonth);		
> 	}
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (HARMONY-4907) [classlib][luni] Calendar.get(Calendar.WEEK_OF_MONTH) returns wrong value

Posted by "Chen Shunfei (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/HARMONY-4907?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Chen Shunfei updated HARMONY-4907:
----------------------------------

    Summary: [classlib][luni] Calendar.get(Calendar.WEEK_OF_MONTH) returns wrong value  (was: [classlib][luni] Calendar.get(Calendar.WEEK_OF_MONTH))

> [classlib][luni] Calendar.get(Calendar.WEEK_OF_MONTH) returns wrong value
> -------------------------------------------------------------------------
>
>                 Key: HARMONY-4907
>                 URL: https://issues.apache.org/jira/browse/HARMONY-4907
>             Project: Harmony
>          Issue Type: Bug
>          Components: Classlib
>         Environment: harmony-jre-579330
> WindowXP
>            Reporter: Chen Shunfei
>
> Here is the testcase:
> import java.util.Calendar;
> import java.util.GregorianCalendar;
> import junit.framework.TestCase;
> public class Test extends TestCase {
> 	public void testDayOfWeekInMonth() {
> 		GregorianCalendar c = new GregorianCalendar();
> 		int minimalDaysInFirstWeek = 2;
> 		int firstDayOfWeek = Calendar.SUNDAY;
> 		c.setFirstDayOfWeek(firstDayOfWeek);
> 		int month = Calendar.DECEMBER;
> 		int year = 2007;
> 		int day = 1;
> 		c.set(year, month, day);
> 		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
> 		int relativeDayOfWeek = (7 + dayOfWeek - firstDayOfWeek) % 7;
> 		int relativeDayOfFirst = (relativeDayOfWeek - day + 1 + 35) % 7;
> 		int weekOfFirst = ((7 - relativeDayOfFirst) >= minimalDaysInFirstWeek) ? 1
> 				: 0;
> 		int weekOfMonth = (day + relativeDayOfFirst - 1) / 7 + weekOfFirst;
> 		assertEquals(c.get(Calendar.WEEK_OF_MONTH), weekOfMonth);		
> 	}
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.