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