Skip to content

Commit

Permalink
ICU-22679 Clean up Calendar code.
Browse files Browse the repository at this point in the history
1. Remove redudant implementation of default system ceuntry by using
   macro
2. Fold long if / else block if one block return.
  • Loading branch information
FrankYFTang committed Mar 21, 2024
1 parent 94305fc commit fbc1f33
Show file tree
Hide file tree
Showing 26 changed files with 198 additions and 858 deletions.
94 changes: 10 additions & 84 deletions icu4c/source/i18n/buddhcal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#if !UCONFIG_NO_FORMATTING

#include "buddhcal.h"
#include "gregoimp.h"
#include "unicode/gregocal.h"
#include "umutex.h"
#include <float.h>
Expand Down Expand Up @@ -70,16 +71,14 @@ int32_t BuddhistCalendar::handleGetExtendedYear(UErrorCode& status)
}
// EXTENDED_YEAR in BuddhistCalendar is a Gregorian year.
// The default value of EXTENDED_YEAR is 1970 (Buddhist 2513)
int32_t year;
if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
year = internalGet(UCAL_EXTENDED_YEAR, kGregorianEpoch);
} else {
// extended year is a gregorian year, where 1 = 1AD, 0 = 1BC, -1 = 2BC, etc
year = internalGet(UCAL_YEAR, kGregorianEpoch - kBuddhistEraStart);
if (uprv_add32_overflow(year, kBuddhistEraStart, &year)) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
return internalGet(UCAL_EXTENDED_YEAR, kGregorianEpoch);
}
// extended year is a gregorian year, where 1 = 1AD, 0 = 1BC, -1 = 2BC, etc
int32_t year = internalGet(UCAL_YEAR, kGregorianEpoch - kBuddhistEraStart);
if (uprv_add32_overflow(year, kBuddhistEraStart, &year)) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
return year;
}
Expand All @@ -96,84 +95,11 @@ int32_t BuddhistCalendar::handleGetLimit(UCalendarDateFields field, ELimitType l
{
if(field == UCAL_ERA) {
return BE;
} else {
return GregorianCalendar::handleGetLimit(field,limitType);
}
}

#if 0
void BuddhistCalendar::timeToFields(UDate theTime, UBool quick, UErrorCode& status)
{
//Calendar::timeToFields(theTime, quick, status);

int32_t era = internalGet(UCAL_ERA);
int32_t year = internalGet(UCAL_YEAR);

if(era == GregorianCalendar::BC) {
year = 1-year;
era = BuddhistCalendar::BE;
} else if(era == GregorianCalendar::AD) {
era = BuddhistCalendar::BE;
} else {
status = U_INTERNAL_PROGRAM_ERROR;
}

year = year - kBuddhistEraStart;

internalSet(UCAL_ERA, era);
internalSet(UCAL_YEAR, year);
}
#endif

/**
* The system maintains a static default century start date. This is initialized
* the first time it is used. Once the system default century date and year
* are set, they do not change.
*/
static UDate gSystemDefaultCenturyStart = DBL_MIN;
static int32_t gSystemDefaultCenturyStartYear = -1;
static icu::UInitOnce gBCInitOnce {};


UBool BuddhistCalendar::haveDefaultCentury() const
{
return true;
}

static void U_CALLCONV
initializeSystemDefaultCentury()
{
// initialize systemDefaultCentury and systemDefaultCenturyYear based
// on the current time. They'll be set to 80 years before
// the current time.
UErrorCode status = U_ZERO_ERROR;
BuddhistCalendar calendar(Locale("@calendar=buddhist"),status);
if (U_SUCCESS(status)) {
calendar.setTime(Calendar::getNow(), status);
calendar.add(UCAL_YEAR, -80, status);
UDate newStart = calendar.getTime(status);
int32_t newYear = calendar.get(UCAL_YEAR, status);
gSystemDefaultCenturyStartYear = newYear;
gSystemDefaultCenturyStart = newStart;
}
// We have no recourse upon failure unless we want to propagate the failure
// out.
}

UDate BuddhistCalendar::defaultCenturyStart() const
{
// lazy-evaluate systemDefaultCenturyStart and systemDefaultCenturyStartYear
umtx_initOnce(gBCInitOnce, &initializeSystemDefaultCentury);
return gSystemDefaultCenturyStart;
}

int32_t BuddhistCalendar::defaultCenturyStartYear() const
{
// lazy-evaluate systemDefaultCenturyStartYear and systemDefaultCenturyStart
umtx_initOnce(gBCInitOnce, &initializeSystemDefaultCentury);
return gSystemDefaultCenturyStartYear;
return GregorianCalendar::handleGetLimit(field,limitType);
}

IMPL_SYSTEM_DEFAULT_CENTURY(BuddhistCalendar, "@calendar=buddhist")

U_NAMESPACE_END

Expand Down
21 changes: 2 additions & 19 deletions icu4c/source/i18n/buddhcal.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,26 +159,9 @@ class BuddhistCalendar : public GregorianCalendar {
*/
virtual int32_t handleGetLimit(UCalendarDateFields field, ELimitType limitType) const override;

/**
* Returns true because the Buddhist Calendar does have a default century
* @internal
*/
virtual UBool haveDefaultCentury() const override;

/**
* Returns the date of the start of the default century
* @return start of century - in milliseconds since epoch, 1970
* @internal
*/
virtual UDate defaultCenturyStart() const override;

/**
* Returns the year in which the default century begins
* @internal
*/
virtual int32_t defaultCenturyStartYear() const override;

virtual bool isEra0CountingBackward() const override { return false; }

DECLARE_OVERRIDE_SYSTEM_DEFAULT_CENTURY
};

U_NAMESPACE_END
Expand Down
14 changes: 6 additions & 8 deletions icu4c/source/i18n/cecal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ CECalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) cons
return LIMITS[field][limitType];
}

UBool
CECalendar::haveDefaultCentury() const
{
return true;
}

//-------------------------------------------------------------------------
// Calendar system Conversion methods...
//-------------------------------------------------------------------------
Expand All @@ -134,13 +128,17 @@ CECalendar::jdToCE(int32_t julianDay, int32_t jdEpochOffset, int32_t& year, int3
static const char* kMonthCode13 = "M13";

const char* CECalendar::getTemporalMonthCode(UErrorCode& status) const {
if (get(UCAL_MONTH, status) == 12) return kMonthCode13;
if (get(UCAL_MONTH, status) == 12) {
return kMonthCode13;
}
return Calendar::getTemporalMonthCode(status);
}

void
CECalendar::setTemporalMonthCode(const char* code, UErrorCode& status) {
if (U_FAILURE(status)) return;
if (U_FAILURE(status)) {
return;
}
if (uprv_strcmp(code, kMonthCode13) == 0) {
set(UCAL_MONTH, 12);
set(UCAL_IS_LEAP_MONTH, 0);
Expand Down
6 changes: 0 additions & 6 deletions icu4c/source/i18n/cecal.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,6 @@ class U_I18N_API CECalendar : public Calendar {
*/
virtual int32_t handleGetLimit(UCalendarDateFields field, ELimitType limitType) const override;

/**
* Returns true because Coptic/Ethiopic Calendar does have a default century
* @internal
*/
virtual UBool haveDefaultCentury() const override;

protected:
/**
* The Coptic and Ethiopic calendars differ only in their epochs.
Expand Down
59 changes: 1 addition & 58 deletions icu4c/source/i18n/chnsecal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -951,64 +951,7 @@ void ChineseCalendar::setRelatedYear(int32_t year)
set(UCAL_EXTENDED_YEAR, year - kChineseRelatedYearDiff);
}

// default century

static UDate gSystemDefaultCenturyStart = DBL_MIN;
static int32_t gSystemDefaultCenturyStartYear = -1;
static icu::UInitOnce gSystemDefaultCenturyInitOnce {};


UBool ChineseCalendar::haveDefaultCentury() const
{
return true;
}

UDate ChineseCalendar::defaultCenturyStart() const
{
return internalGetDefaultCenturyStart();
}

int32_t ChineseCalendar::defaultCenturyStartYear() const
{
return internalGetDefaultCenturyStartYear();
}

namespace { // anonymous

static void U_CALLCONV initializeSystemDefaultCentury()
{
// initialize systemDefaultCentury and systemDefaultCenturyYear based
// on the current time. They'll be set to 80 years before
// the current time.
UErrorCode status = U_ZERO_ERROR;
ChineseCalendar calendar(Locale("@calendar=chinese"),status);
if (U_SUCCESS(status)) {
calendar.setTime(Calendar::getNow(), status);
calendar.add(UCAL_YEAR, -80, status);
gSystemDefaultCenturyStart = calendar.getTime(status);
gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status);
}
// We have no recourse upon failure unless we want to propagate the failure
// out.
}

} // namespace

UDate
ChineseCalendar::internalGetDefaultCenturyStart() const
{
// lazy-evaluate systemDefaultCenturyStart
umtx_initOnce(gSystemDefaultCenturyInitOnce, &initializeSystemDefaultCentury);
return gSystemDefaultCenturyStart;
}

int32_t
ChineseCalendar::internalGetDefaultCenturyStartYear() const
{
// lazy-evaluate systemDefaultCenturyStartYear
umtx_initOnce(gSystemDefaultCenturyInitOnce, &initializeSystemDefaultCentury);
return gSystemDefaultCenturyStartYear;
}
IMPL_SYSTEM_DEFAULT_CENTURY(ChineseCalendar, "@calendar=chinese")

bool
ChineseCalendar::inTemporalLeapYear(UErrorCode &status) const
Expand Down
30 changes: 1 addition & 29 deletions icu4c/source/i18n/chnsecal.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,39 +279,11 @@ class U_I18N_API ChineseCalendar : public Calendar {
virtual int32_t internalGetMonth(UErrorCode& status) const override;

protected:
/**
* Returns true because the Islamic Calendar does have a default century
* @internal
*/
virtual UBool haveDefaultCentury() const override;

/**
* Returns the date of the start of the default century
* @return start of century - in milliseconds since epoch, 1970
* @internal
*/
virtual UDate defaultCenturyStart() const override;

/**
* Returns the year in which the default century begins
* @internal
*/
virtual int32_t defaultCenturyStartYear() const override;
DECLARE_OVERRIDE_SYSTEM_DEFAULT_CENTURY

private: // default century stuff.

/**
* Returns the beginning date of the 100-year window that dates
* with 2-digit years are considered to fall within.
*/
UDate internalGetDefaultCenturyStart() const;

/**
* Returns the first year of the 100-year window that dates with
* 2-digit years are considered to fall within.
*/
int32_t internalGetDefaultCenturyStartYear() const;

ChineseCalendar() = delete; // default constructor not implemented

#ifdef __CalendarTest__
Expand Down
66 changes: 13 additions & 53 deletions icu4c/source/i18n/coptccal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#if !UCONFIG_NO_FORMATTING

#include "gregoimp.h"
#include "umutex.h"
#include "coptccal.h"
#include "cecal.h"
Expand Down Expand Up @@ -62,22 +63,19 @@ CopticCalendar::handleGetExtendedYear(UErrorCode& status)
if (U_FAILURE(status)) {
return 0;
}
int32_t eyear;
if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
eyear = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
} else {
// The year defaults to the epoch start, the era to CE
int32_t era = internalGet(UCAL_ERA, CE);
if (era == BCE) {
eyear = 1 - internalGet(UCAL_YEAR, 1); // Convert to extended year
} else if (era == CE){
eyear = internalGet(UCAL_YEAR, 1); // Default to year 1
} else {
status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
return internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
}
// The year defaults to the epoch start, the era to CE
int32_t era = internalGet(UCAL_ERA, CE);
if (era == BCE) {
return 1 - internalGet(UCAL_YEAR, 1); // Convert to extended year
}
return eyear;
if (era == CE){
return internalGet(UCAL_YEAR, 1); // Default to year 1
}
status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}

void
Expand Down Expand Up @@ -120,45 +118,7 @@ void CopticCalendar::setRelatedYear(int32_t year)
set(UCAL_EXTENDED_YEAR, year - kCopticRelatedYearDiff);
}

/**
* The system maintains a static default century start date and Year. They are
* initialized the first time they are used. Once the system default century date
* and year are set, they do not change.
*/
static UDate gSystemDefaultCenturyStart = DBL_MIN;
static int32_t gSystemDefaultCenturyStartYear = -1;
static icu::UInitOnce gSystemDefaultCenturyInit {};


static void U_CALLCONV initializeSystemDefaultCentury() {
UErrorCode status = U_ZERO_ERROR;
CopticCalendar calendar(Locale("@calendar=coptic"), status);
if (U_SUCCESS(status)) {
calendar.setTime(Calendar::getNow(), status);
calendar.add(UCAL_YEAR, -80, status);
gSystemDefaultCenturyStart = calendar.getTime(status);
gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status);
}
// We have no recourse upon failure unless we want to propagate the failure
// out.
}

UDate
CopticCalendar::defaultCenturyStart() const
{
// lazy-evaluate systemDefaultCenturyStart
umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
return gSystemDefaultCenturyStart;
}

int32_t
CopticCalendar::defaultCenturyStartYear() const
{
// lazy-evaluate systemDefaultCenturyStart
umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
return gSystemDefaultCenturyStartYear;
}

IMPL_SYSTEM_DEFAULT_CENTURY(CopticCalendar, "@calendar=coptic")

int32_t
CopticCalendar::getJDEpochOffset() const
Expand Down
Loading

0 comments on commit fbc1f33

Please sign in to comment.