• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #include "i18n_calendar.h"
16 
17 #include "buddhcal.h"
18 #include "chnsecal.h"
19 #include "coptccal.h"
20 #include "ethpccal.h"
21 #include "hebrwcal.h"
22 #include "indiancal.h"
23 #include "islamcal.h"
24 #include "japancal.h"
25 #include "locdspnm.h"
26 #include "persncal.h"
27 #include "string"
28 #include "ureslocs.h"
29 #include "ulocimp.h"
30 #include "umachine.h"
31 #include "unicode/gregocal.h"
32 #include "unicode/timezone.h"
33 #include "unistr.h"
34 #include "urename.h"
35 #include "ustr_imp.h"
36 #include "ustring.h"
37 
38 namespace OHOS {
39 namespace Global {
40 namespace I18n {
I18nCalendar(std::string localeTag)41 I18nCalendar::I18nCalendar(std::string localeTag)
42 {
43     UErrorCode status = U_ZERO_ERROR;
44     icu::Locale tempLocale = icu::Locale::forLanguageTag(localeTag, status);
45     if (status != U_ZERO_ERROR) {
46         calendar_ = new icu::GregorianCalendar(status);
47         if (!U_SUCCESS(status)) {
48             if (calendar_ != nullptr) {
49                 delete calendar_;
50             }
51             calendar_ = nullptr;
52         }
53         return;
54     }
55     calendar_ = icu::Calendar::createInstance(tempLocale, status);
56     if (status != U_ZERO_ERROR) {
57         if (calendar_ != nullptr) {
58             delete calendar_;
59         }
60         calendar_ = nullptr;
61     }
62 }
63 
I18nCalendar(std::string localeTag,CalendarType type)64 I18nCalendar::I18nCalendar(std::string localeTag, CalendarType type)
65 {
66     UErrorCode status = U_ZERO_ERROR;
67     icu::Locale tempLocale = icu::Locale::forLanguageTag(localeTag, status);
68     if (status != U_ZERO_ERROR) {
69         calendar_ = new icu::GregorianCalendar(status);
70         if (!U_SUCCESS(status)) {
71             if (calendar_ != nullptr) {
72                 delete calendar_;
73             }
74             calendar_ = nullptr;
75         }
76         return;
77     }
78     InitCalendar(tempLocale, type);
79 }
80 
InitCalendar(const icu::Locale & locale,CalendarType type)81 void I18nCalendar::InitCalendar(const icu::Locale &locale, CalendarType type)
82 {
83     UErrorCode status = U_ZERO_ERROR;
84     switch (type) {
85         case BUDDHIST: {
86             calendar_ = new icu::BuddhistCalendar(locale, status);
87             break;
88         }
89         case CHINESE: {
90             calendar_ = new icu::ChineseCalendar(locale, status);
91             break;
92         }
93         case COPTIC: {
94             calendar_ = new icu::CopticCalendar(locale, status);
95             break;
96         }
97         case ETHIOPIC: {
98             calendar_ = new icu::EthiopicCalendar(locale, status);
99             break;
100         }
101         case HEBREW: {
102             calendar_ = new icu::HebrewCalendar(locale, status);
103             break;
104         }
105         case INDIAN: {
106             calendar_ = new icu::IndianCalendar(locale, status);
107             break;
108         }
109         case ISLAMIC_CIVIL: {
110             calendar_ = new icu::IslamicCalendar(locale, status, icu::IslamicCalendar::ECalculationType::CIVIL);
111             break;
112         }
113         default: {
114             InitCalendar2(locale, type, status);
115         }
116     }
117     if (!U_SUCCESS(status)) {
118         if (calendar_ != nullptr) {
119             delete calendar_;
120         }
121         calendar_ = nullptr;
122     }
123 }
124 
InitCalendar2(const icu::Locale & locale,CalendarType type,UErrorCode & status)125 void I18nCalendar::InitCalendar2(const icu::Locale &locale, CalendarType type, UErrorCode &status)
126 {
127     switch (type) {
128         case ISLAMIC_TBLA: {
129             calendar_ = new icu::IslamicCalendar(locale, status, icu::IslamicCalendar::ECalculationType::TBLA);
130             break;
131         }
132         case ISLAMIC_UMALQURA: {
133             calendar_ = new icu::IslamicCalendar(locale, status, icu::IslamicCalendar::ECalculationType::UMALQURA);
134             break;
135         }
136         case JAPANESE: {
137             calendar_ = new icu::JapaneseCalendar(locale, status);
138             break;
139         }
140         case PERSIAN: {
141             calendar_ = new icu::PersianCalendar(locale, status);
142             break;
143         }
144         case GREGORY: {
145             calendar_ = new icu::GregorianCalendar(locale, status);
146             break;
147         }
148         default: {
149             calendar_ = icu::Calendar::createInstance(locale, status);
150         }
151     }
152 }
153 
~I18nCalendar()154 I18nCalendar::~I18nCalendar()
155 {
156     if (calendar_ != nullptr) {
157         delete calendar_;
158     }
159 }
160 
SetTime(double value)161 void I18nCalendar::SetTime(double value)
162 {
163     if (calendar_ != nullptr) {
164         UErrorCode status = U_ZERO_ERROR;
165         calendar_->setTime(value, status);
166         return;
167     }
168 }
169 
SetTimeZone(std::string id)170 void I18nCalendar::SetTimeZone(std::string id)
171 {
172     icu::UnicodeString zone = icu::UnicodeString::fromUTF8(id);
173     icu::TimeZone *timezone = icu::TimeZone::createTimeZone(zone);
174     if (!timezone) {
175         return;
176     }
177     if (calendar_ != nullptr) {
178         calendar_->setTimeZone(*timezone);
179     }
180     delete(timezone);
181 }
182 
GetTimeZone(void)183 std::string I18nCalendar::GetTimeZone(void)
184 {
185     std::string ret;
186     if (calendar_) {
187         icu::UnicodeString unistr;
188         calendar_->getTimeZone().getID(unistr);
189         unistr.toUTF8String<std::string>(ret);
190     }
191     return ret;
192 }
193 
Set(int32_t year,int32_t month,int32_t date)194 void I18nCalendar::Set(int32_t year, int32_t month, int32_t date)
195 {
196     if (calendar_ != nullptr) {
197         calendar_->set(year, month, date);
198         return;
199     }
200 }
201 
Set(UCalendarDateFields field,int32_t value)202 void I18nCalendar::Set(UCalendarDateFields field, int32_t value)
203 {
204     if (calendar_ != nullptr) {
205         calendar_->set(field, value);
206         return;
207     }
208 }
209 
Get(UCalendarDateFields field) const210 int32_t I18nCalendar::Get(UCalendarDateFields field) const
211 {
212     if (calendar_ != nullptr) {
213         UErrorCode status = U_ZERO_ERROR;
214         return calendar_->get(field, status);
215     }
216     return 0;
217 }
218 
Add(UCalendarDateFields field,int32_t amount)219 void I18nCalendar::Add(UCalendarDateFields field, int32_t amount)
220 {
221     if (calendar_ != nullptr) {
222         UErrorCode status = U_ZERO_ERROR;
223         calendar_->add(field, amount, status);
224     }
225 }
226 
SetMinimalDaysInFirstWeek(int32_t value)227 void I18nCalendar::SetMinimalDaysInFirstWeek(int32_t value)
228 {
229     if (calendar_ != nullptr) {
230         calendar_->setMinimalDaysInFirstWeek((uint8_t)value);
231         return;
232     }
233 }
234 
SetFirstDayOfWeek(int32_t value)235 void I18nCalendar::SetFirstDayOfWeek(int32_t value)
236 {
237     if (value < UCalendarDaysOfWeek::UCAL_SUNDAY || value > UCAL_SATURDAY) {
238         return;
239     }
240     if (calendar_ != nullptr) {
241         calendar_->setFirstDayOfWeek(UCalendarDaysOfWeek(value));
242         return;
243     }
244 }
245 
GetTimeInMillis(void)246 UDate I18nCalendar::GetTimeInMillis(void)
247 {
248     if (calendar_ != nullptr) {
249         UErrorCode status = U_ZERO_ERROR;
250         return calendar_->getTime(status);
251     }
252     return 0;
253 }
254 
GetMinimalDaysInFirstWeek(void)255 int32_t I18nCalendar::GetMinimalDaysInFirstWeek(void)
256 {
257     if (calendar_ != nullptr) {
258         return calendar_->getMinimalDaysInFirstWeek();
259     }
260     return 1;
261 }
262 
GetFirstDayOfWeek(void)263 int32_t I18nCalendar::GetFirstDayOfWeek(void)
264 {
265     if (calendar_ != nullptr) {
266         return static_cast<int>(calendar_->getFirstDayOfWeek());
267     }
268     return UCAL_SUNDAY;
269 }
270 
IsWeekend(int64_t date,UErrorCode & status)271 bool I18nCalendar::IsWeekend(int64_t date, UErrorCode &status)
272 {
273     if (calendar_ != nullptr) {
274         return calendar_->isWeekend(date, status);
275     }
276     return false;
277 }
278 
IsWeekend(void)279 bool I18nCalendar::IsWeekend(void)
280 {
281     if (calendar_ != nullptr) {
282         return calendar_->isWeekend();
283     }
284     return false;
285 }
286 
GetDisplayName(std::string & displayLocaleTag)287 std::string I18nCalendar::GetDisplayName(std::string &displayLocaleTag)
288 {
289     if (calendar_ == nullptr) {
290         return "";
291     }
292     const char *type = calendar_->getType();
293     if (type == nullptr) {
294         return "";
295     }
296     UErrorCode status = U_ZERO_ERROR;
297     icu::Locale displayLocale = icu::Locale::forLanguageTag(displayLocaleTag, status);
298     if (status != U_ZERO_ERROR) {
299         return "";
300     }
301     icu::LocaleDisplayNames *dspName = icu::LocaleDisplayNames::createInstance(displayLocale);
302     icu::UnicodeString unistr;
303     dspName->keyValueDisplayName("calendar", type, unistr);
304     delete dspName;
305     std::string ret;
306     unistr.toUTF8String<std::string>(ret);
307     return ret;
308 }
309 } // namespace I18n
310 } // namespace Global
311 } // namespace OHOS