ICU-4611 Fix Japanese calendar calculation problem. Changed internal APIs to get default month/date of a calendar without updating calendar fields.

X-SVN-Rev: 24574
This commit is contained in:
Yoshito Umaoka 2008-09-15 21:24:11 +00:00
parent ff17b981a5
commit b3744287df
5 changed files with 22 additions and 23 deletions

View file

@ -2511,7 +2511,7 @@ int32_t Calendar::handleComputeJulianDay(UCalendarDateFields bestField) {
if(isSet(UCAL_MONTH)) {
month = internalGet(UCAL_MONTH);
} else {
month = getDefaultMonthInYear();
month = getDefaultMonthInYear(year);
}
int32_t julianDay = handleComputeMonthStart(year, useMonth ? month : 0, useMonth);
@ -2523,7 +2523,7 @@ int32_t Calendar::handleComputeJulianDay(UCalendarDateFields bestField) {
if(isSet(UCAL_DAY_OF_MONTH)) {
dayOfMonth = internalGet(UCAL_DAY_OF_MONTH,1);
} else {
dayOfMonth = getDefaultDayInMonth(month);
dayOfMonth = getDefaultDayInMonth(year, month);
}
return julianDay + dayOfMonth;
}
@ -2678,13 +2678,13 @@ int32_t Calendar::handleComputeJulianDay(UCalendarDateFields bestField) {
}
int32_t
Calendar::getDefaultMonthInYear()
Calendar::getDefaultMonthInYear(int32_t /*eyear*/)
{
return 0;
}
int32_t
Calendar::getDefaultDayInMonth(int32_t /*month*/)
Calendar::getDefaultDayInMonth(int32_t /*eyear*/, int32_t /*month*/)
{
return 1;
}

View file

@ -318,19 +318,17 @@ const char *JapaneseCalendar::getType() const
return "japanese";
}
int32_t JapaneseCalendar::getDefaultMonthInYear()
int32_t JapaneseCalendar::getDefaultMonthInYear(int32_t eyear)
{
UErrorCode status = U_ZERO_ERROR;
int32_t era = internalGetEra();
computeFields(status); // slow
int32_t year = getGregorianYear();
// TODO do we assume we can trust 'era'? What if it is denormalized?
int32_t month = GregorianCalendar::getDefaultMonthInYear();
int32_t month = 0;
// Find out if we are at the edge of an era
if(year == kEraInfo[era].year) {
if(eyear == kEraInfo[era].year) {
// Yes, we're in the first year of this era.
return kEraInfo[era].month-1;
}
@ -338,15 +336,13 @@ int32_t JapaneseCalendar::getDefaultMonthInYear()
return month;
}
int32_t JapaneseCalendar::getDefaultDayInMonth(int32_t month)
int32_t JapaneseCalendar::getDefaultDayInMonth(int32_t eyear, int32_t month)
{
UErrorCode status = U_ZERO_ERROR;
int32_t era = internalGetEra();
computeFields(status); // slow
int32_t year = getGregorianYear();
int32_t day = GregorianCalendar::getDefaultDayInMonth(month);
int32_t day = 1;
if(year == kEraInfo[era].year) {
if(eyear == kEraInfo[era].year) {
if(month == (kEraInfo[era].month-1)) {
return kEraInfo[era].day;
}

View file

@ -192,17 +192,20 @@ protected:
* Called by computeJulianDay. Returns the default month (0-based) for the year,
* taking year and era into account. Will return the first month of the given era, if
* the current year is an ascension year.
* @param eyear the extended year
* @internal
*/
virtual int32_t getDefaultMonthInYear();
virtual int32_t getDefaultMonthInYear(int32_t eyear);
/***
* Called by computeJulianDay. Returns the default day (1-based) for the month,
* taking currently-set year and era into account. Will return the first day of the given
* era, if the current month is an ascension year and month.
* @param eyear the extended year
* @param mon the month in the year
* @internal
*/
virtual int32_t getDefaultDayInMonth(int32_t month);
virtual int32_t getDefaultDayInMonth(int32_t eyear, int32_t month);
};
U_NAMESPACE_END

View file

@ -1715,18 +1715,20 @@ protected:
/**
* Called by computeJulianDay. Returns the default month (0-based) for the year,
* taking year and era into account. Defaults to 0 for Gregorian, which doesn't care.
* @internal
* @param eyear The extended year
* @internal
*/
virtual int32_t getDefaultMonthInYear() ;
virtual int32_t getDefaultMonthInYear(int32_t /*eyear*/) ;
/**
* Called by computeJulianDay. Returns the default day (1-based) for the month,
* taking currently-set year and era into account. Defaults to 1 for Gregorian.
* @param eyear the extended year
* @param mon the month in the year
* @internal
*/
virtual int32_t getDefaultDayInMonth(int32_t /*month*/);
virtual int32_t getDefaultDayInMonth(int32_t /*eyear*/, int32_t /*month*/);
//-------------------------------------------------------------------------
// Protected utility methods for use by subclasses. These are very handy

View file

@ -670,8 +670,6 @@ void IntlCalendarTest::TestJapanese3860()
}
}
#if 0
// this will NOT work - *all the time*. If it is the 1st of the month, for example it will get Jan 1 heisei 1 => jan 1 showa 64, wrong era.
{
// Test simple parse/format with adopt
UDate aDate = 0;
@ -701,7 +699,7 @@ void IntlCalendarTest::TestJapanese3860()
int32_t gotYear = cal2->get(UCAL_YEAR, s2);
int32_t gotEra = cal2->get(UCAL_ERA, s2);
int32_t expectYear = 1;
int32_t expectEra = JapaneseCalendar::kCurrentEra;
int32_t expectEra = 235; //JapaneseCalendar::kCurrentEra;
if((gotYear!=1) || (gotEra != expectEra)) {
errln(UnicodeString("parse "+samplestr+" of 'y' as Japanese Calendar, expected year ") + expectYear +
UnicodeString(" and era ") + expectEra +", but got year " + gotYear + " and era " + gotEra + " (Gregorian:" + str +")");
@ -711,7 +709,7 @@ void IntlCalendarTest::TestJapanese3860()
delete fmt;
}
}
#endif
delete cal2;
delete cal;
delete fmt2;