Coverage Report

Created: 2026-08-14 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/icu/icu4c/source/i18n/erarules.h
Line
Count
Source
1
// © 2018 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
4
#ifndef ERARULES_H_
5
#define ERARULES_H_
6
7
#include "unicode/utypes.h"
8
9
#if !UCONFIG_NO_FORMATTING
10
11
#include "unicode/localpointer.h"
12
#include "unicode/uobject.h"
13
#include "unicode/unistr.h"
14
#include "cmemory.h"
15
16
9.71k
#define MAX_CAL_ID_LENGTH 31    // Currently, maximum calendar type length is 19 
17
18
U_NAMESPACE_BEGIN
19
20
class U_I18N_API_CLASS EraRules : public UMemory {
21
public:
22
    U_I18N_API ~EraRules();
23
24
    U_I18N_API static EraRules* createInstance(const char* calType,
25
                                               UBool includeTentativeEra,
26
                                               UErrorCode& status);
27
    /**
28
     * Gets the calendar type ID
29
     * @return  calendar type ID
30
     */
31
29.1k
    inline const char* getCalendarType() const {
32
29.1k
        return calTypeId;
33
29.1k
    }
34
35
    /**
36
     * Gets the era rules of the calendar from which the current calendar inherits eras,
37
     * or nullptr if there is no such calendar.
38
     * @return  era rules of the calendar from which the current calendar inherits eras,
39
     * or nullptr.
40
     */
41
29.1k
    inline const EraRules* getInheritEraRules() const {
42
29.1k
        return inheritEraRules;
43
29.1k
    }
44
45
    /**
46
     * Gets number of effective eras
47
     * @return  number of effective eras (not the same as max era code)
48
     */
49
0
    inline int32_t getNumberOfEras() const {
50
0
        return inheritEraRules == nullptr ? numEras : numEras + inheritEraRules->getNumberOfEras();
51
0
    }
52
53
    /**
54
     * Gets maximum defined era code for the current calendar
55
     * @return  maximum defined era code
56
     */
57
59.6k
    inline int32_t getMaxEraCode() const {
58
59.6k
        return minEra + startDatesLength - 1;
59
59.6k
    }
60
61
    /**
62
     * Gets minimum defined era code for the current calendar
63
     * @return  minimum defined era code
64
     */
65
434
    inline int32_t getMinEraCode() const {
66
434
        int minEraCode = minEra;
67
434
        if (inheritEraRules != nullptr) {
68
0
            minEraCode = inheritEraRules->getMinEraCode();
69
0
        }
70
434
        return minEraCode;
71
434
    }
72
73
    /**
74
     * Gets next era code
75
     * @param eraCode Current era code
76
     * @return Next era code. If not available, maximum era code is returned.
77
     */
78
    U_I18N_API int32_t getNextEraCode(int32_t eraCode) const;
79
80
    /**
81
     * Gets start date of an era
82
     * @param eraCode   Era code
83
     * @param fields    Receives date fields. The result includes values of year, month,
84
     *                  day of month in this order. When an era has no start date, the result
85
     *                  will be January 1st in year whose value is minimum integer.
86
     * @param status    Receives status.
87
     */
88
    U_I18N_API void getStartDate(int32_t eraCode, int32_t (&fields)[3], UErrorCode& status) const;
89
90
    /**
91
     * Gets start year of an era
92
     * @param eraCode   Era code
93
     * @param status    Receives status.
94
     * @return The first year of an era. When a era has no start date, minimum int32
95
     *          value is returned.
96
     */
97
    U_I18N_API int32_t getStartYear(int32_t eraCode, UErrorCode& status) const;
98
99
    /**
100
     * Returns era code for the specified year/month/day.
101
     * @param year  Year
102
     * @param month Month (1-base)
103
     * @param day   Day of month
104
     * @param status    Receives status
105
     * @return  era code (or code of earliest era when date is before that era)
106
     */
107
    U_I18N_API int32_t getEraCode(int32_t year, int32_t month, int32_t day, UErrorCode& status) const;
108
109
    /**
110
     * Gets the current era code. This is calculated only once for an instance of
111
     * EraRules. The current era calculation is based on the default time zone at
112
     * the time of instantiation.
113
     *
114
     * @return era code of current era (or era code of earliest era when current date is before any era)
115
     */
116
1
    inline int32_t getCurrentEraCode() const {
117
1
        return currentEra;
118
1
    }
119
120
private:
121
    EraRules(const char* calTypeId, LocalMemory<int32_t>& startDatesIn, int32_t startDatesLengthIn,
122
        int32_t minEraIn, int32_t numErasIn);
123
124
    void initCurrentEra();
125
126
    char calTypeId[MAX_CAL_ID_LENGTH + 1];  // calendar type id
127
    LocalMemory<int32_t> startDates;
128
    int32_t startDatesLength;
129
    int32_t minEra;  // minimum valid era code, for first entry in startDates
130
    int32_t numEras; // number of valid era codes (not necessarily the same as startDates length
131
    int32_t currentEra;
132
    EraRules* inheritEraRules;
133
};
134
135
U_NAMESPACE_END
136
#endif /* #if !UCONFIG_NO_FORMATTING */
137
#endif /* ERARULES_H_ */