1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef OHOS_GLOBAL_I18N_LUNAR_CALENDAR_H
16 #define OHOS_GLOBAL_I18N_LUNAR_CALENDAR_H
17 
18 #include <string>
19 #include <unordered_map>
20 #include <vector>
21 
22 #include "unicode/calendar.h"
23 #include "unicode/ucal.h"
24 #include "unicode/utypes.h"
25 
26 namespace OHOS {
27 namespace Global {
28 namespace I18n {
29 class LunarCalendar {
30 public:
31     LunarCalendar();
32     ~LunarCalendar();
33     bool SetGregorianDate(int32_t year, int32_t month, int32_t day);
34     int32_t GetLunarYear();
35     int32_t GetLunarMonth();
36     int32_t GetLunarDay();
37     bool IsLeapMonth();
38 
39 private:
40     bool VerifyDate(int32_t year, int32_t month, int32_t day);
41     void ConvertDate(int32_t& year, int32_t& month, int32_t& day);
42     void CalcDaysFromBaseDate();
43     void SolorDateToLunarDate();
44     void AdjustLeapMonth(int32_t& i, int32_t tempDaysCounts, int32_t leapMonth);
45     int32_t GetDaysPerLunarYear(int32_t lunarYear);
46     bool IsGregorianLeapYear(int32_t year);
47     static std::unordered_map<int32_t, int32_t> daysOfMonth;
48     static std::unordered_map<int32_t, int32_t> accDaysOfMonth;
49     static std::vector<uint32_t> lunarDateInfo;
50     static const int32_t VALID_START_YEAR = 1900;
51     static const int32_t VALID_END_YEAR = 2100;
52     static const int32_t VALID_START_MONTH = 1;
53     static const int32_t VALID_END_MONTH = 12;
54     static const int32_t VALID_START_DAY = 1;
55     static const int32_t MONTH_FEB = 2;
56     static const int32_t START_YEAR = 1900;
57     static const int32_t END_YEAR = 2100;
58     static const int32_t DAYS_OF_YEAR = 365;
59     static const int32_t YEAR_ERA = 100;
60     static const int32_t FREQ_LEAP_YEAR = 4;
61     static const int32_t DAYS_FROM_SOLAR_TO_LUNAR = 30;
62     static const int32_t BASE_DAYS_PER_LUNAR_YEAR = 348;
63     static const int32_t DAYS_IN_BIG_MONTH = 30;
64     static const int32_t DAYS_IN_SMALL_MONTH = 29;
65 
66     int32_t solorYear = -1;
67     int32_t solorMonth = -1;
68     int32_t solorDay = -1;
69     int32_t lunarYear = -1;
70     int32_t lunarMonth = -1;
71     int32_t lunarDay = -1;
72     int32_t daysCounts = 0;
73     bool isLeapMonth = false;
74     bool isGregorianLeapYear = false;
75     bool isValidDate = false;
76     icu::Calendar* calendar_;
77 };
78 } // namespace I18n
79 } // namespace Global
80 } // namespace OHOS
81 #endif