• 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 
16 #ifndef OHOS_GLOBAL_I18N_ZONE_UTIL_H
17 #define OHOS_GLOBAL_I18N_ZONE_UTIL_H
18 
19 #include <string>
20 #include <unordered_map>
21 #include <vector>
22 #include "phonenumbers/phonenumberutil.h"
23 #include "strenum.h"
24 
25 namespace OHOS {
26 namespace Global {
27 namespace I18n {
28 class ZoneUtil {
29 public:
30     /**
31      * default constructor
32      */
ZoneUtil()33     ZoneUtil() : phone_util(*i18n::phonenumbers::PhoneNumberUtil::GetInstance()) {}
34 
35     /**
36      * deconstructor
37      */
~ZoneUtil()38     ~ZoneUtil() {}
39 
40     /**
41      * @brief Get the default timezone for the given country
42      *
43      * @param country Indicating the country code
44      * @return Returns the default timezone if the country code is valid, otherwise
45      * returns an empty string.
46      */
47     std::string GetDefaultZone(const std::string &country);
48 
49     /**
50      * @brief Get the default timezone for the given region code
51      *
52      * @param number Indicating the region code, for example 86 can
53      * be used to retrieve the default timezone of China.
54      * @return Returns the default timezone name if the region code is valid, otherwise
55      * returns an empty string.
56      */
57     std::string GetDefaultZone(const int32_t number);
58 
59     /**
60      * @brief Get the default timezone name for the given country code
61      *
62      * @param country Indicating the country code
63      * @param offset Indicating the offset from GMT(in milliseconds)
64      * @return Returns the default timezone name if the country code is valid, otherwise
65      * returns an empty string.
66      */
67     std::string GetDefaultZone(const std::string country, const int32_t offset);
68 
69     /**
70      * @brief Get the default timezone name for the given region code
71      *
72      * @param number Indicating the region code, for example 86 can
73      * be used to retrieve the default timezone of China.
74      * @param offset Indicating the offset from GMT(in milliseconds).
75      * @return Returns the default timezone name if the country code is valid, otherwise
76      * returns an empty string.
77      */
78     std::string GetDefaultZone(const int32_t number, const int32_t offset);
79 
80     /**
81      * @brief Get the timezone list for the given country code
82      *
83      * @param country Indicating the country code
84      * @param retVec used to store the returned timezones
85      */
86     void GetZoneList(const std::string country, std::vector<std::string> &retVec);
87 
88     /**
89      * @brief Get the timezone list for the given country code
90      *
91      * @param country Indicating the country code
92      * @param offset Indicating the offset from GMT(in milliseconds)
93      * @param retVec used to store the returned timezones
94      */
95     void GetZoneList(const std::string country, const int32_t offset, std::vector<std::string> &retVec);
96 private:
97     const i18n::phonenumbers::PhoneNumberUtil &phone_util;
98     static std::unordered_map<std::string, std::string> defaultMap;
99     static bool icuInitialized;
100     static void GetList(icu::StringEnumeration *strEnum, std::vector<std::string> &ret);
101     static void GetString(icu::StringEnumeration *strEnum, std::string &ret);
102     static bool Init();
103 };
104 } // namespace I18n
105 } // namespace Global
106 } // namespace OHOS
107 #endif
108