Skip to content

Commit

Permalink
ICU-22991 Use bit fields and initialize with field description
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankYFTang committed Jan 4, 2025
1 parent 8969894 commit 4cc9027
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 76 deletions.
54 changes: 22 additions & 32 deletions icu4c/source/i18n/calendar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,16 +696,13 @@ static const char gGregorian[] = "gregorian";

Calendar::Calendar(UErrorCode& success)
: UObject(),
fIsTimeSet(false),
fAreFieldsSet(false),
fAreAllFieldsSet(false),
fAreFieldsVirtuallySet(false),
fNextStamp(kMinimumUserStamp),
fTime(0),
fLenient(true),
fZone(nullptr),
fRepeatedWallTime(UCAL_WALLTIME_LAST),
fSkippedWallTime(UCAL_WALLTIME_LAST)
fIsTimeSet(false),
fAreFieldsSet(false),
fAreAllFieldsSet(false),
fAreFieldsVirtuallySet(false),
fLenient(true),
fRepeatedWallTime(UCAL_WALLTIME_LAST),
fSkippedWallTime(UCAL_WALLTIME_LAST)
{
validLocale[0] = 0;
actualLocale[0] = 0;
Expand All @@ -724,16 +721,13 @@ fSkippedWallTime(UCAL_WALLTIME_LAST)

Calendar::Calendar(TimeZone* zone, const Locale& aLocale, UErrorCode& success)
: UObject(),
fIsTimeSet(false),
fAreFieldsSet(false),
fAreAllFieldsSet(false),
fAreFieldsVirtuallySet(false),
fNextStamp(kMinimumUserStamp),
fTime(0),
fLenient(true),
fZone(nullptr),
fRepeatedWallTime(UCAL_WALLTIME_LAST),
fSkippedWallTime(UCAL_WALLTIME_LAST)
fIsTimeSet(false),
fAreFieldsSet(false),
fAreAllFieldsSet(false),
fAreFieldsVirtuallySet(false),
fLenient(true),
fRepeatedWallTime(UCAL_WALLTIME_LAST),
fSkippedWallTime(UCAL_WALLTIME_LAST)
{
validLocale[0] = 0;
actualLocale[0] = 0;
Expand All @@ -759,16 +753,13 @@ fSkippedWallTime(UCAL_WALLTIME_LAST)

Calendar::Calendar(const TimeZone& zone, const Locale& aLocale, UErrorCode& success)
: UObject(),
fIsTimeSet(false),
fAreFieldsSet(false),
fAreAllFieldsSet(false),
fAreFieldsVirtuallySet(false),
fNextStamp(kMinimumUserStamp),
fTime(0),
fLenient(true),
fZone(nullptr),
fRepeatedWallTime(UCAL_WALLTIME_LAST),
fSkippedWallTime(UCAL_WALLTIME_LAST)
fIsTimeSet(false),
fAreFieldsSet(false),
fAreAllFieldsSet(false),
fAreFieldsVirtuallySet(false),
fLenient(true),
fRepeatedWallTime(UCAL_WALLTIME_LAST),
fSkippedWallTime(UCAL_WALLTIME_LAST)
{
validLocale[0] = 0;
actualLocale[0] = 0;
Expand All @@ -795,7 +786,6 @@ Calendar::~Calendar()
Calendar::Calendar(const Calendar &source)
: UObject(source)
{
fZone = nullptr;
*this = source;
}

Expand Down Expand Up @@ -4229,7 +4219,7 @@ Calendar::recalculateStamp() {
int32_t currentValue;
int32_t j, i;

fNextStamp = 1;
fNextStamp = kInternallySet;

for (j = 0; j < UCAL_FIELD_COUNT; j++) {
currentValue = STAMP_MAX;
Expand Down
86 changes: 42 additions & 44 deletions icu4c/source/i18n/unicode/calendar.h
Original file line number Diff line number Diff line change
Expand Up @@ -1881,38 +1881,6 @@ class U_I18N_API Calendar : public UObject {
*/
int32_t getActualHelper(UCalendarDateFields field, int32_t startValue, int32_t endValue, UErrorCode &status) const;


private:
/**
* The flag which indicates if the current time is set in the calendar.
*/
UBool fIsTimeSet;

/**
* True if the fields are in sync with the currently set time of this Calendar.
* If false, then the next attempt to get the value of a field will
* force a recomputation of all fields from the current value of the time
* field.
* <P>
* This should really be named areFieldsInSync, but the old name is retained
* for backward compatibility.
*/
UBool fAreFieldsSet;

/**
* True if all of the fields have been set. This is initially false, and set to
* true by computeFields().
*/
UBool fAreAllFieldsSet;

/**
* True if all fields have been virtually set, but have not yet been
* computed. This occurs only in setTimeInMillis(). A calendar set
* to this state will compute all fields from the time if it becomes
* necessary, but otherwise will delay such computation.
*/
UBool fAreFieldsVirtuallySet;

protected:
/**
* Get the current time without recomputing.
Expand Down Expand Up @@ -2169,7 +2137,7 @@ class U_I18N_API Calendar : public UObject {
/**
* The next available value for fStamp[]
*/
int8_t fNextStamp;// = MINIMUM_USER_STAMP;
int8_t fNextStamp = kMinimumUserStamp;

/**
* Recalculates the time stamp array (fStamp).
Expand All @@ -2180,30 +2148,60 @@ class U_I18N_API Calendar : public UObject {
/**
* The current time set for the calendar.
*/
UDate fTime;
UDate fTime = 0;

/**
* @see #setLenient
* Time zone affects the time calculation done by Calendar. Calendar subclasses use
* the time zone data to produce the local time. Always set; never nullptr.
*/
UBool fLenient;
TimeZone* fZone = nullptr;

/**
* Time zone affects the time calculation done by Calendar. Calendar subclasses use
* the time zone data to produce the local time. Always set; never nullptr.
* The flag which indicates if the current time is set in the calendar.
*/
bool fIsTimeSet:1;

/**
* True if the fields are in sync with the currently set time of this Calendar.
* If false, then the next attempt to get the value of a field will
* force a recomputation of all fields from the current value of the time
* field.
* <P>
* This should really be named areFieldsInSync, but the old name is retained
* for backward compatibility.
*/
bool fAreFieldsSet:1;

/**
* True if all of the fields have been set. This is initially false, and set to
* true by computeFields().
*/
bool fAreAllFieldsSet:1;

/**
* True if all fields have been virtually set, but have not yet been
* computed. This occurs only in setTimeInMillis(). A calendar set
* to this state will compute all fields from the time if it becomes
* necessary, but otherwise will delay such computation.
*/
bool fAreFieldsVirtuallySet:1;

/**
* @see #setLenient
*/
TimeZone* fZone;
bool fLenient:1;

/**
* Option for repeated wall time
* @see #setRepeatedWallTimeOption
*/
UCalendarWallTimeOption fRepeatedWallTime;
UCalendarWallTimeOption fRepeatedWallTime:2;

/**
* Option for skipped wall time
* @see #setSkippedWallTimeOption
*/
UCalendarWallTimeOption fSkippedWallTime;
UCalendarWallTimeOption fSkippedWallTime:2;

/**
* Both firstDayOfWeek and minimalDaysInFirstWeek are locale-dependent. They are
Expand All @@ -2213,11 +2211,11 @@ class U_I18N_API Calendar : public UObject {
* out the week count for a specific date for a given locale. These must be set when
* a Calendar is constructed.
*/
UCalendarDaysOfWeek fFirstDayOfWeek;
UCalendarDaysOfWeek fFirstDayOfWeek:3;
UCalendarDaysOfWeek fWeekendOnset:3;
UCalendarDaysOfWeek fWeekendCease:3;
uint8_t fMinimalDaysInFirstWeek;
UCalendarDaysOfWeek fWeekendOnset;
int32_t fWeekendOnsetMillis;
UCalendarDaysOfWeek fWeekendCease;
int32_t fWeekendCeaseMillis;

/**
Expand Down

0 comments on commit 4cc9027

Please sign in to comment.