• 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 "libxml/globals.h"
23 #include "libxml/tree.h"
24 #include "libxml/xmlstring.h"
25 #include "unicode/strenum.h"
26 
27 namespace OHOS {
28 namespace Global {
29 namespace I18n {
30 struct NITZData {
31     // smaller than 0 means null, 0 means false, 1 means true
32     int32_t isDST;
33     int32_t totalOffset;
34     int64_t currentMillis;
35 };
36 
37 enum MatchQuality {
38     DEFAULT_BOOSTED = 0,
39     SINGLE_ZONE,
40     MULTIPLE_ZONES_SAME_OFFSET,
41     MULTIPLE_ZONES_DIFFERENT_OFFSET
42 };
43 
44 struct CountryResult {
45     bool isOnlyMatch;
46     MatchQuality quality;
47     std::string timezoneId;
48 };
49 
50 class ZoneUtil {
51 public:
52     /**
53      * default constructor
54      */
ZoneUtil()55     ZoneUtil() {}
56 
57     /**
58      * deconstructor
59      */
~ZoneUtil()60     ~ZoneUtil() {}
61 
62     /**
63      * @brief Get the default timezone for the given country
64      *
65      * @param country Indicating the country code
66      * @return Returns the default timezone if the country code is valid, otherwise
67      * returns an empty string.
68      */
69     std::string GetDefaultZone(const std::string& country);
70 
71     /**
72      * @brief Get the default timezone for the given region code
73      *
74      * @param number Indicating the region code, for example 86 can
75      * be used to retrieve the default timezone of China.
76      * @return Returns the default timezone name if the region code is valid, otherwise
77      * returns an empty string.
78      */
79     std::string GetDefaultZone(int32_t number);
80 
81     /**
82      * @brief Get the default timezone name for the given country code
83      *
84      * @param country Indicating the country code
85      * @param offset Indicating the offset from GMT(in milliseconds)
86      * @return Returns the default timezone name if the country code is valid, otherwise
87      * returns an empty string.
88      */
89     std::string GetDefaultZone(const std::string& country, int32_t offset);
90 
91     /**
92      * @brief Get the default timezone name for the given region code
93      *
94      * @param number Indicating the region code, for example 86 can
95      * be used to retrieve the default timezone of China.
96      * @param offset Indicating the offset from GMT(in milliseconds).
97      * @return Returns the default timezone name if the country code is valid, otherwise
98      * returns an empty string.
99      */
100     std::string GetDefaultZone(int32_t number, int32_t offset);
101 
102     /**
103      * @brief Get the timezone list for the given country code
104      *
105      * @param country Indicating the country code
106      * @param retVec used to store the returned timezones
107      */
108     void GetZoneList(const std::string& country, std::vector<std::string>& retVec);
109 
110     /**
111      * @brief Get the timezone list for the given country code
112      *
113      * @param country Indicating the country code
114      * @param offset Indicating the offset from GMT(in milliseconds)
115      * @param retVec used to store the returned timezones
116      */
117     void GetZoneList(const std::string& country, int32_t offset, std::vector<std::string> &retVec);
118 
119     /**
120      * @brief Get recommended timezone by country and nitz information.
121      *
122      * @param region Indicating the country code
123      * @param nitzData Indicating the nitz information
124      * @return Returns the CountryResult object which contains recommended timezone id and match information.
125      */
126     CountryResult LookupTimezoneByCountryAndNITZ(std::string &region, NITZData &nitzData);
127 
128     /**
129      * @brief Get recommended timezone nitz information. This method is used when country code is not available.
130      *
131      * @param nitzData Indicating the nitz information
132      * @return Returns the CountryResult object which contains recommended timezone id and match information.
133      */
134     CountryResult LookupTimezoneByNITZ(NITZData &nitzData);
135 
136     /**
137      * @brief Get recommended timezone by country information.
138      *
139      * @param region Indicating the country code
140      * @param currentMillis Indicating the current utc time.
141      * @return Returns the CountryResult object which contains recommended timezone id and match information.
142      */
143     CountryResult LookupTimezoneByCountry(std::string &region, int64_t currentMillis);
144 
145 private:
146     static std::unordered_map<std::string, std::string> defaultMap;
147     static bool icuInitialized;
148     static void GetList(icu::StringEnumeration *strEnum, std::vector<std::string> &ret);
149     static void GetString(icu::StringEnumeration *strEnum, std::string &ret);
150     static bool Init();
151     bool CheckFileExist();
152     bool CheckSameDstOffset(std::vector<std::string> &zones, std::string &defaultTimezone,
153         int64_t currentMillis);
154     void GetTimezones(xmlNodePtr &value, std::vector<std::string> &zones);
155     void GetDefaultAndBoost(xmlNodePtr &value, std::string &defaultTimezone, bool &isBoosted,
156         std::vector<std::string> &zones);
157     bool IsFindCountry(xmlDocPtr &doc, xmlNodePtr &cur, xmlNodePtr &value, std::string &region);
158     void GetCountryZones(std::string &region, std::string &defaultTimezone, bool &isBoosted,
159         std::vector<std::string> &zones);
160     void GetICUCountryZones(std::string &region, std::vector<std::string> &zones, std::string &defaultTimezone);
161     CountryResult Match(std::vector<std::string> &zones, NITZData &nitzData, std::string &systemTimezone);
162     static const char *GetTZLookupDataPath();
163 
164     static const char *COUNTRY_ZONE_DATA_PATH;
165     static const char *DISTRO_COUNTRY_ZONE_DATA_PATH;
166     static const char *DEFAULT_TIMEZONE;
167     static const char *TIMEZONES_TAG;
168     static const char *ID_TAG;
169     static const char *DEFAULT_TAG;
170     static const char *BOOSTED_TAG;
171     static const char *ROOT_TAG;
172     static const char *SECOND_TAG;
173     static const char *CODE_TAG;
174     static const char *TIMEZONE_KEY;
175     static constexpr int SYS_PARAM_LEN = 128;
176     static const char *TZLOOKUP_FILE_PATH;
177 };
178 } // namespace I18n
179 } // namespace Global
180 } // namespace OHOS
181 #endif
182