1 /*
2 * Copyright (c) 2022-2023 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_timezone.h"
16
17 #include <filesystem>
18 #include <fstream>
19 #include <sys/stat.h>
20 #include "hilog/log.h"
21 #include "libxml/globals.h"
22 #include "libxml/tree.h"
23 #include "libxml/xmlstring.h"
24 #include "locale_config.h"
25 #include "locale_info.h"
26 #include "map"
27 #include "set"
28 #include "string"
29 #include "type_traits"
30 #include "umachine.h"
31 #include "utility"
32 #include "utils.h"
33 #include "utypes.h"
34 #include "vector"
35 #include "unicode/locid.h"
36 #include "unicode/unistr.h"
37 #include "utils.h"
38 #include <cstdio>
39 #include <cstdlib>
40 #include <iostream>
41 #include <regex>
42
43 namespace OHOS {
44 namespace Global {
45 namespace I18n {
46 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD001E00, "I18nJs" };
47 using namespace OHOS::HiviewDFX;
48 using namespace std::filesystem;
49
50 const char *I18nTimeZone::TIMEZONE_KEY = "persist.time.timezone";
51 const char *I18nTimeZone::DEFAULT_TIMEZONE = "GMT";
52
53 const char *I18nTimeZone::CITY_TIMEZONE_DATA_PATH = "/system/usr/ohos_timezone/ohos_timezones.xml";
54 const char *I18nTimeZone::DEVICE_CITY_TIMEZONE_DATA_PATH = "/system/usr/ohos_timezone/device_timezones.xml";
55 const char *I18nTimeZone::TZ_PIXEL_PATH = "/system/usr/ohos_timezone";
56 const char *I18nTimeZone::DEFAULT_LOCALE = "root";
57 const char *I18nTimeZone::CITY_DISPLAYNAME_PATH = "/system/usr/ohos_timezone/ohos_city_dispname/";
58 const char *I18nTimeZone::DEVICE_CITY_DISPLAYNAME_PATH = "/system/usr/ohos_timezone/device_city_dispname/";
59
60 const char *I18nTimeZone::TIMEZONE_ROOT_TAG = "timezones";
61 const char *I18nTimeZone::TIMEZONE_SECOND_ROOT_TAG = "timezone";
62 const char *I18nTimeZone::CITY_DISPLAYNAME_ROOT_TAG = "display_names";
63 const char *I18nTimeZone::CITY_DISPLAYNAME_SECOND_ROOT_TAG = "display_name";
64 const char *I18nTimeZone::ZONEINFO_PATH = "/system/etc/zoneinfo";
65
66 std::set<std::string> I18nTimeZone::availableIDs {};
67 std::set<std::string> I18nTimeZone::supportedLocales {};
68 std::set<std::string> I18nTimeZone::availableZoneCityIDs {};
69 std::map<std::string, std::string> I18nTimeZone::city2TimeZoneID {};
70 bool I18nTimeZone::useDeviceCityDispName = false;
71
72 std::map<int, std::string> I18nTimeZone::categoryNum2TimezoneWN {
73 { 0, "Africa/Abidjan" },
74 { 1, "Africa/Accra" },
75 { 2, "Africa/Algiers" },
76 { 3, "Africa/Bamako" },
77 { 4, "Africa/Banjul" },
78 { 5, "Africa/Bissau" },
79 { 6, "Africa/Casablanca" },
80 { 7, "Africa/Ceuta" },
81 { 8, "Africa/Conakry" },
82 { 9, "Africa/Dakar" },
83 { 10, "Africa/El_Aaiun" },
84 { 11, "Africa/Freetown" },
85 { 12, "Africa/Lome" },
86 { 13, "Africa/Monrovia" },
87 { 14, "Africa/Nouakchott" },
88 { 15, "Africa/Ouagadougou" },
89 { 16, "America/Adak" },
90 { 17, "America/Anchorage" },
91 { 18, "America/Anguilla" },
92 { 19, "America/Antigua" },
93 { 20, "America/Aruba" },
94 { 21, "America/Atikokan" },
95 { 22, "America/Bahia_Banderas" },
96 { 23, "America/Barbados" },
97 { 24, "America/Belem" },
98 { 25, "America/Belize" },
99 { 26, "America/Blanc-Sablon" },
100 { 27, "America/Boa_Vista" },
101 { 28, "America/Bogota" },
102 { 29, "America/Boise" },
103 { 30, "America/Cambridge_Bay" },
104 { 31, "America/Cancun" },
105 { 32, "America/Caracas" },
106 { 33, "America/Cayenne" },
107 { 34, "America/Cayman" },
108 { 35, "America/Chicago" },
109 { 36, "America/Chihuahua" },
110 { 37, "America/Mexico_City" }, // old data is 'America/Ciudad_Juarez'
111 { 38, "America/Costa_Rica" },
112 { 39, "America/Creston" },
113 { 40, "America/Curacao" },
114 { 41, "America/Danmarkshavn" },
115 { 42, "America/Dawson" },
116 { 43, "America/Dawson_Creek" },
117 { 44, "America/Denver" },
118 { 45, "America/Detroit" },
119 { 46, "America/Dominica" },
120 { 47, "America/Edmonton" },
121 { 48, "America/El_Salvador" },
122 { 49, "America/Fort_Nelson" },
123 { 50, "America/Glace_Bay" },
124 { 51, "America/Goose_Bay" },
125 { 52, "America/Grand_Turk" },
126 { 53, "America/Grenada" },
127 { 54, "America/Guadeloupe" },
128 { 55, "America/Guatemala" },
129 { 56, "America/Guayaquil" },
130 { 57, "America/Guyana" },
131 { 58, "America/Halifax" },
132 { 59, "America/Havana" },
133 { 60, "America/Hermosillo" },
134 { 61, "America/Indiana/Indianapolis" },
135 { 62, "America/Indiana/Knox" },
136 { 63, "America/Indiana/Marengo" },
137 { 64, "America/Indiana/Petersburg" },
138 { 65, "America/Indiana/Tell_City" },
139 { 66, "America/Indiana/Vevay" },
140 { 67, "America/Indiana/Vincennes" },
141 { 68, "America/Indiana/Winamac" },
142 { 69, "America/Inuvik" },
143 { 70, "America/Iqaluit" },
144 { 71, "America/Jamaica" },
145 { 72, "America/Juneau" },
146 { 73, "America/Kentucky/Louisville" },
147 { 74, "America/Kentucky/Monticello" },
148 { 75, "America/Kralendijk" },
149 { 76, "America/Los_Angeles" },
150 { 77, "America/Lower_Princes" },
151 { 78, "America/Managua" },
152 { 79, "America/Manaus" },
153 { 80, "America/Marigot" },
154 { 81, "America/Martinique" },
155 { 82, "America/Matamoros" },
156 { 83, "America/Mazatlan" },
157 { 84, "America/Menominee" },
158 { 85, "America/Merida" },
159 { 86, "America/Metlakatla" },
160 { 87, "America/Mexico_City" },
161 { 88, "America/Miquelon" },
162 { 89, "America/Moncton" },
163 { 90, "America/Monterrey" },
164 { 91, "America/Montserrat" },
165 { 92, "America/Nassau" },
166 { 93, "America/New_York" },
167 { 94, "America/Nome" },
168 { 95, "America/Noronha" },
169 { 96, "America/North_Dakota/Beulah" },
170 { 97, "America/North_Dakota/Center" },
171 { 98, "America/North_Dakota/New_Salem" },
172 { 99, "America/Nuuk" },
173 { 100, "America/Ojinaga" },
174 { 101, "America/Panama" },
175 { 102, "America/Paramaribo" },
176 { 103, "America/Phoenix" },
177 { 104, "America/Port-au-Prince" },
178 { 105, "America/Port_of_Spain" },
179 { 106, "America/Puerto_Rico" },
180 { 107, "America/Rankin_Inlet" },
181 { 108, "America/Regina" },
182 { 109, "America/Resolute" },
183 { 110, "America/Santarem" },
184 { 111, "America/Santo_Domingo" },
185 { 112, "America/Scoresbysund" },
186 { 113, "America/Sitka" },
187 { 114, "America/St_Barthelemy" },
188 { 115, "America/St_Johns" },
189 { 116, "America/St_Kitts" },
190 { 117, "America/St_Lucia" },
191 { 118, "America/St_Thomas" },
192 { 119, "America/St_Vincent" },
193 { 120, "America/Swift_Current" },
194 { 121, "America/Tegucigalpa" },
195 { 122, "America/Thule" },
196 { 123, "America/Tijuana" },
197 { 124, "America/Toronto" },
198 { 125, "America/Tortola" },
199 { 126, "America/Vancouver" },
200 { 127, "America/Whitehorse" },
201 { 128, "America/Winnipeg" },
202 { 129, "America/Yakutat" },
203 { 130, "Asia/Anadyr" },
204 { 131, "Atlantic/Azores" },
205 { 132, "Atlantic/Bermuda" },
206 { 133, "Atlantic/Canary" },
207 { 134, "Atlantic/Cape_Verde" },
208 { 135, "Atlantic/Faroe" },
209 { 136, "Atlantic/Madeira" },
210 { 137, "Atlantic/Reykjavik" },
211 { 138, "Etc/GMT" },
212 { 139, "Etc/GMT+1" },
213 { 140, "Etc/GMT+10" },
214 { 141, "Etc/GMT+11" },
215 { 142, "Etc/GMT+12" },
216 { 143, "Etc/GMT+2" },
217 { 144, "Etc/GMT+3" },
218 { 145, "Etc/GMT+4" },
219 { 146, "Etc/GMT+5" },
220 { 147, "Etc/GMT+6" },
221 { 148, "Etc/GMT+7" },
222 { 149, "Etc/GMT+8" },
223 { 150, "Etc/GMT+9" },
224 { 151, "Etc/GMT-12" },
225 { 152, "Europe/Dublin" },
226 { 153, "Europe/Gibraltar" },
227 { 154, "Europe/Guernsey" },
228 { 155, "Europe/Isle_of_Man" },
229 { 156, "Europe/Jersey" },
230 { 157, "Europe/Lisbon" },
231 { 158, "Europe/London" },
232 { 159, "Europe/Madrid" },
233 { 160, "Europe/Oslo" },
234 { 161, "Europe/Paris" },
235 { 162, "Pacific/Galapagos" },
236 { 163, "Pacific/Honolulu" },
237 { 164, "Pacific/Kiritimati" },
238 { 165, "Pacific/Midway" },
239 };
240 std::map<int, std::string> I18nTimeZone::categoryNum2TimezoneEN {
241 { 0, "Africa/Accra" },
242 { 1, "Africa/Addis_Ababa" },
243 { 2, "Africa/Algiers" },
244 { 3, "Africa/Asmara" },
245 { 4, "Africa/Bamako" },
246 { 5, "Africa/Bangui" },
247 { 6, "Africa/Brazzaville" },
248 { 7, "Africa/Cairo" },
249 { 8, "Africa/Djibouti" },
250 { 9, "Africa/Douala" },
251 { 10, "Africa/Juba" },
252 { 11, "Africa/Kampala" },
253 { 12, "Africa/Khartoum" },
254 { 13, "Africa/Kinshasa" },
255 { 14, "Africa/Lagos" },
256 { 15, "Africa/Libreville" },
257 { 16, "Africa/Lome" },
258 { 17, "Africa/Lubumbashi" },
259 { 18, "Africa/Malabo" },
260 { 19, "Africa/Mogadishu" },
261 { 20, "Africa/Nairobi" },
262 { 21, "Africa/Ndjamena" },
263 { 22, "Africa/Niamey" },
264 { 23, "Africa/Ouagadougou" },
265 { 24, "Africa/Porto-Novo" },
266 { 25, "Africa/Sao_Tome" },
267 { 26, "Africa/Tripoli" },
268 { 27, "Africa/Tunis" },
269 { 28, "America/Adak" },
270 { 29, "Arctic/Longyearbyen" },
271 { 30, "Asia/Aden" },
272 { 31, "Asia/Almaty" },
273 { 32, "Asia/Amman" },
274 { 33, "Asia/Anadyr" },
275 { 34, "Asia/Aqtau" },
276 { 35, "Asia/Aqtobe" },
277 { 36, "Asia/Ashgabat" },
278 { 37, "Asia/Atyrau" },
279 { 38, "Asia/Baghdad" },
280 { 39, "Asia/Bahrain" },
281 { 40, "Asia/Baku" },
282 { 41, "Asia/Bangkok" },
283 { 42, "Asia/Barnaul" },
284 { 43, "Asia/Beirut" },
285 { 44, "Asia/Bishkek" },
286 { 45, "Asia/Brunei" },
287 { 46, "Asia/Chita" },
288 { 47, "Asia/Choibalsan" },
289 { 48, "Asia/Colombo" },
290 { 49, "Asia/Damascus" },
291 { 50, "Asia/Dhaka" },
292 { 51, "Asia/Dubai" },
293 { 52, "Asia/Dushanbe" },
294 { 53, "Asia/Famagusta" },
295 { 54, "Asia/Gaza" },
296 { 55, "Asia/Hebron" },
297 { 56, "Asia/Ho_Chi_Minh" },
298 { 57, "Asia/Hong_Kong" },
299 { 58, "Asia/Hovd" },
300 { 59, "Asia/Irkutsk" },
301 { 60, "Asia/Jakarta" },
302 { 61, "Asia/Jayapura" },
303 { 62, "Asia/Jerusalem" },
304 { 63, "Asia/Kabul" },
305 { 64, "Asia/Kamchatka" },
306 { 65, "Asia/Karachi" },
307 { 66, "Asia/Kathmandu" },
308 { 67, "Asia/Khandyga" },
309 { 68, "Asia/Kolkata" },
310 { 69, "Asia/Krasnoyarsk" },
311 { 70, "Asia/Kuala_Lumpur" },
312 { 71, "Asia/Kuching" },
313 { 72, "Asia/Kuwait" },
314 { 73, "Asia/Macau" },
315 { 74, "Asia/Magadan" },
316 { 75, "Asia/Makassar" },
317 { 76, "Asia/Manila" },
318 { 77, "Asia/Muscat" },
319 { 78, "Asia/Nicosia" },
320 { 79, "Asia/Novokuznetsk" },
321 { 80, "Asia/Novosibirsk" },
322 { 81, "Asia/Omsk" },
323 { 82, "Asia/Oral" },
324 { 83, "Asia/Phnom_Penh" },
325 { 84, "Asia/Pontianak" },
326 { 85, "Asia/Pyongyang" },
327 { 86, "Asia/Qatar" },
328 { 87, "Asia/Qostanay" },
329 { 88, "Asia/Qyzylorda" },
330 { 89, "Asia/Riyadh" },
331 { 90, "Asia/Sakhalin" },
332 { 91, "Asia/Samarkand" },
333 { 92, "Asia/Seoul" },
334 { 93, "Asia/Shanghai" },
335 { 94, "Asia/Singapore" },
336 { 95, "Asia/Srednekolymsk" },
337 { 96, "Asia/Taipei" },
338 { 97, "Asia/Tashkent" },
339 { 98, "Asia/Tbilisi" },
340 { 99, "Asia/Tehran" },
341 { 100, "Asia/Thimphu" },
342 { 101, "Asia/Tokyo" },
343 { 102, "Asia/Tomsk" },
344 { 103, "Asia/Ulaanbaatar" },
345 { 104, "Asia/Urumqi" },
346 { 105, "Asia/Ust-Nera" },
347 { 106, "Asia/Vientiane" },
348 { 107, "Asia/Vladivostok" },
349 { 108, "Asia/Yakutsk" },
350 { 109, "Asia/Yangon" },
351 { 110, "Asia/Yekaterinburg" },
352 { 111, "Asia/Yerevan" },
353 { 112, "Etc/GMT" },
354 { 113, "Etc/GMT-1" },
355 { 114, "Etc/GMT-10" },
356 { 115, "Etc/GMT-11" },
357 { 116, "Etc/GMT-12" },
358 { 117, "Etc/GMT-2" },
359 { 118, "Etc/GMT-3" },
360 { 119, "Etc/GMT-4" },
361 { 120, "Etc/GMT-5" },
362 { 121, "Etc/GMT-6" },
363 { 122, "Etc/GMT-7" },
364 { 123, "Etc/GMT-8" },
365 { 124, "Etc/GMT-9" },
366 { 125, "Europe/Amsterdam" },
367 { 126, "Europe/Andorra" },
368 { 127, "Europe/Astrakhan" },
369 { 128, "Europe/Athens" },
370 { 129, "Europe/Belgrade" },
371 { 130, "Europe/Berlin" },
372 { 131, "Europe/Bratislava" },
373 { 132, "Europe/Brussels" },
374 { 133, "Europe/Bucharest" },
375 { 134, "Europe/Budapest" },
376 { 135, "Europe/Busingen" },
377 { 136, "Europe/Chisinau" },
378 { 137, "Europe/Copenhagen" },
379 { 138, "Europe/Helsinki" },
380 { 139, "Europe/Istanbul" },
381 { 140, "Europe/Kaliningrad" },
382 { 141, "Europe/Kirov" },
383 { 142, "Europe/Kiev" },
384 { 143, "Europe/Ljubljana" },
385 { 144, "Europe/London" },
386 { 145, "Europe/Luxembourg" },
387 { 146, "Europe/Madrid" },
388 { 147, "Europe/Malta" },
389 { 148, "Europe/Mariehamn" },
390 { 149, "Europe/Minsk" },
391 { 150, "Europe/Monaco" },
392 { 151, "Europe/Moscow" },
393 { 152, "Europe/Oslo" },
394 { 153, "Europe/Paris" },
395 { 154, "Europe/Podgorica" },
396 { 155, "Europe/Prague" },
397 { 156, "Europe/Riga" },
398 { 157, "Europe/Rome" },
399 { 158, "Europe/Samara" },
400 { 159, "Europe/San_Marino" },
401 { 160, "Europe/Sarajevo" },
402 { 161, "Europe/Saratov" },
403 { 162, "Europe/Simferopol" },
404 { 163, "Europe/Skopje" },
405 { 164, "Europe/Sofia" },
406 { 165, "Europe/Stockholm" },
407 { 166, "Europe/Tallinn" },
408 { 167, "Europe/Tirane" },
409 { 168, "Europe/Ulyanovsk" },
410 { 169, "Europe/Vaduz" },
411 { 170, "Europe/Vienna" },
412 { 171, "Europe/Vilnius" },
413 { 172, "Europe/Volgograd" },
414 { 173, "Europe/Warsaw" },
415 { 174, "Europe/Zagreb" },
416 { 175, "Europe/Zurich" },
417 { 176, "Indian/Maldives" },
418 { 177, "Pacific/Chuuk" },
419 { 178, "Pacific/Guam" },
420 { 179, "Pacific/Kosrae" },
421 { 180, "Pacific/Kwajalein" },
422 { 181, "Pacific/Majuro" },
423 { 182, "Pacific/Palau" },
424 { 183, "Pacific/Pohnpei" },
425 { 184, "Pacific/Saipan" },
426 { 185, "Pacific/Tarawa" },
427 { 186, "Pacific/Wake" },
428 };
429 std::map<int, std::string> I18nTimeZone::categoryNum2TimezoneWS {
430 { 0, "Africa/Johannesburg" },
431 { 1, "America/Araguaina" },
432 { 2, "America/Argentina/Buenos_Aires" },
433 { 3, "America/Argentina/Catamarca" },
434 { 4, "America/Argentina/Cordoba" },
435 { 5, "America/Argentina/Jujuy" },
436 { 6, "America/Argentina/La_Rioja" },
437 { 7, "America/Argentina/Mendoza" },
438 { 8, "America/Argentina/Rio_Gallegos" },
439 { 9, "America/Argentina/Salta" },
440 { 10, "America/Argentina/San_Juan" },
441 { 11, "America/Argentina/San_Luis" },
442 { 12, "America/Argentina/Tucuman" },
443 { 13, "America/Argentina/Ushuaia" },
444 { 14, "America/Asuncion" },
445 { 15, "America/Bahia" },
446 { 16, "America/Belem" },
447 { 17, "America/Boa_Vista" },
448 { 18, "America/Bogota" },
449 { 19, "America/Campo_Grande" },
450 { 20, "America/Cuiaba" },
451 { 21, "America/Eirunepe" },
452 { 22, "America/Fortaleza" },
453 { 23, "America/Guayaquil" },
454 { 24, "America/La_Paz" },
455 { 25, "America/Lima" },
456 { 26, "America/Maceio" },
457 { 27, "America/Manaus" },
458 { 28, "America/Montevideo" },
459 { 29, "America/Noronha" },
460 { 30, "America/Porto_Velho" },
461 { 31, "America/Punta_Arenas" },
462 { 32, "America/Recife" },
463 { 33, "America/Rio_Branco" },
464 { 34, "America/Santarem" },
465 { 35, "America/Santiago" },
466 { 36, "America/Sao_Paulo" },
467 { 37, "Antarctica/McMurdo" },
468 { 38, "Antarctica/Palmer" },
469 { 39, "Antarctica/Rothera" },
470 { 40, "Atlantic/South_Georgia" },
471 { 41, "Atlantic/St_Helena" },
472 { 42, "Atlantic/Stanley" },
473 { 43, "Etc/GMT" },
474 { 44, "Etc/GMT+1" },
475 { 45, "Etc/GMT+10" },
476 { 46, "Etc/GMT+11" },
477 { 47, "Etc/GMT+12" },
478 { 48, "Etc/GMT+2" },
479 { 49, "Etc/GMT+3" },
480 { 50, "Etc/GMT+4" },
481 { 51, "Etc/GMT+5" },
482 { 52, "Etc/GMT+6" },
483 { 53, "Etc/GMT+7" },
484 { 54, "Etc/GMT+8" },
485 { 55, "Etc/GMT+9" },
486 { 56, "Etc/GMT-12" },
487 { 57, "Etc/UTC" },
488 { 58, "Pacific/Apia" },
489 { 59, "Pacific/Auckland" },
490 { 60, "Pacific/Chatham" },
491 { 61, "Pacific/Easter" },
492 { 62, "Pacific/Fakaofo" },
493 { 63, "Pacific/Fiji" },
494 { 64, "Pacific/Funafuti" },
495 { 65, "Pacific/Galapagos" },
496 { 66, "Pacific/Gambier" },
497 { 67, "Pacific/Kanton" },
498 { 68, "Pacific/Kiritimati" },
499 { 69, "Pacific/Marquesas" },
500 { 70, "Pacific/Niue" },
501 { 71, "Pacific/Pago_Pago" },
502 { 72, "Pacific/Pitcairn" },
503 { 73, "Pacific/Rarotonga" },
504 { 74, "Pacific/Tahiti" },
505 { 75, "Pacific/Tongatapu" },
506 { 76, "Pacific/Wallis" },
507 };
508 std::map<int, std::string> I18nTimeZone::categoryNum2TimezoneES {
509 { 0, "Africa/Blantyre" },
510 { 1, "Africa/Brazzaville" },
511 { 2, "Africa/Bujumbura" },
512 { 3, "Africa/Dar_es_Salaam" },
513 { 4, "Africa/Gaborone" },
514 { 5, "Africa/Harare" },
515 { 6, "Africa/Johannesburg" },
516 { 7, "Africa/Kampala" },
517 { 8, "Africa/Kigali" },
518 { 9, "Africa/Kinshasa" },
519 { 10, "Africa/Libreville" },
520 { 11, "Africa/Luanda" },
521 { 12, "Africa/Lubumbashi" },
522 { 13, "Africa/Lusaka" },
523 { 14, "Africa/Malabo" },
524 { 15, "Africa/Maputo" },
525 { 16, "Africa/Maseru" },
526 { 17, "Africa/Mbabane" },
527 { 18, "Africa/Mogadishu" },
528 { 19, "Africa/Nairobi" },
529 { 20, "Africa/Sao_Tome" },
530 { 21, "Africa/Windhoek" },
531 { 22, "Antarctica/Casey" },
532 { 23, "Antarctica/Davis" },
533 { 24, "Antarctica/DumontDUrville" },
534 { 25, "Antarctica/Macquarie" },
535 { 26, "Antarctica/Mawson" },
536 { 27, "Antarctica/McMurdo" },
537 { 28, "Antarctica/Syowa" },
538 { 29, "Antarctica/Troll" },
539 { 30, "Antarctica/Vostok" },
540 { 31, "Asia/Dili" },
541 { 32, "Asia/Jakarta" },
542 { 33, "Asia/Jayapura" },
543 { 34, "Asia/Makassar" },
544 { 35, "Asia/Pontianak" },
545 { 36, "Australia/Adelaide" },
546 { 37, "Australia/Brisbane" },
547 { 38, "Australia/Broken_Hill" },
548 { 39, "Australia/Darwin" },
549 { 40, "Australia/Eucla" },
550 { 41, "Australia/Hobart" },
551 { 42, "Australia/Lindeman" },
552 { 43, "Australia/Lord_Howe" },
553 { 44, "Australia/Melbourne" },
554 { 45, "Australia/Perth" },
555 { 46, "Australia/Sydney" },
556 { 47, "Etc/GMT" },
557 { 48, "Etc/GMT-1" },
558 { 49, "Etc/GMT-10" },
559 { 50, "Etc/GMT-11" },
560 { 51, "Etc/GMT-12" },
561 { 52, "Etc/GMT-2" },
562 { 53, "Etc/GMT-3" },
563 { 54, "Etc/GMT-4" },
564 { 55, "Etc/GMT-5" },
565 { 56, "Etc/GMT-6" },
566 { 57, "Etc/GMT-7" },
567 { 58, "Etc/GMT-8" },
568 { 59, "Etc/GMT-9" },
569 { 60, "Indian/Antananarivo" },
570 { 61, "Indian/Chagos" },
571 { 62, "Indian/Christmas" },
572 { 63, "Indian/Cocos" },
573 { 64, "Indian/Comoro" },
574 { 65, "Indian/Kerguelen" },
575 { 66, "Indian/Mahe" },
576 { 67, "Indian/Maldives" },
577 { 68, "Indian/Mauritius" },
578 { 69, "Indian/Mayotte" },
579 { 70, "Indian/Reunion" },
580 { 71, "Pacific/Auckland" },
581 { 72, "Pacific/Bougainville" },
582 { 73, "Pacific/Efate" },
583 { 74, "Pacific/Fiji" },
584 { 75, "Pacific/Funafuti" },
585 { 76, "Pacific/Guadalcanal" },
586 { 77, "Pacific/Nauru" },
587 { 78, "Pacific/Norfolk" },
588 { 79, "Pacific/Noumea" },
589 { 80, "Pacific/Port_Moresby" },
590 { 81, "Pacific/Tarawa" },
591 };
592
I18nTimeZone(std::string & id,bool isZoneID)593 I18nTimeZone::I18nTimeZone(std::string &id, bool isZoneID)
594 {
595 if (isZoneID) {
596 if (id.empty()) {
597 std::string systemTimezone = ReadSystemParameter(TIMEZONE_KEY, SYS_PARAM_LEN);
598 if (systemTimezone.length() == 0) {
599 systemTimezone = DEFAULT_TIMEZONE;
600 }
601 icu::UnicodeString unicodeZoneID(systemTimezone.data(), systemTimezone.length());
602 timezone = icu::TimeZone::createTimeZone(unicodeZoneID);
603 } else {
604 icu::UnicodeString unicodeZoneID(id.data(), id.length());
605 timezone = icu::TimeZone::createTimeZone(unicodeZoneID);
606 }
607 } else {
608 if (city2TimeZoneID.size() == 0) {
609 GetAvailableZoneCityIDs();
610 }
611 if (city2TimeZoneID.find(id) == city2TimeZoneID.end()) {
612 timezone = icu::TimeZone::createDefault();
613 } else {
614 std::string timezoneID = city2TimeZoneID.at(id);
615 icu::UnicodeString unicodeZoneID(timezoneID.data(), timezoneID.length());
616 timezone = icu::TimeZone::createTimeZone(unicodeZoneID);
617 }
618 }
619 }
620
~I18nTimeZone()621 I18nTimeZone::~I18nTimeZone()
622 {
623 if (timezone != nullptr) {
624 delete timezone;
625 timezone = nullptr;
626 }
627 }
628
GetTimeZone()629 icu::TimeZone* I18nTimeZone::GetTimeZone()
630 {
631 return timezone;
632 }
633
CreateInstance(std::string & id,bool isZoneID)634 std::unique_ptr<I18nTimeZone> I18nTimeZone::CreateInstance(std::string &id, bool isZoneID)
635 {
636 std::unique_ptr<I18nTimeZone> i18nTimeZone = std::make_unique<I18nTimeZone>(id, isZoneID);
637 if (i18nTimeZone->GetTimeZone() == nullptr) {
638 return nullptr;
639 }
640 return i18nTimeZone;
641 }
642
GetOffset(double date)643 int32_t I18nTimeZone::GetOffset(double date)
644 {
645 int32_t rawOffset = 0;
646 int32_t dstOffset = 0;
647 bool local = false;
648 UErrorCode status = U_ZERO_ERROR;
649 if (timezone == nullptr) {
650 return 0;
651 }
652 timezone->getOffset(date, (UBool)local, rawOffset, dstOffset, status);
653 if (status != U_ZERO_ERROR) {
654 return 0;
655 }
656 return rawOffset + dstOffset;
657 }
658
GetRawOffset()659 int32_t I18nTimeZone::GetRawOffset()
660 {
661 if (timezone == nullptr) {
662 return 0;
663 }
664 return timezone->getRawOffset();
665 }
666
GetID()667 std::string I18nTimeZone::GetID()
668 {
669 if (timezone == nullptr) {
670 return "";
671 }
672 icu::UnicodeString zoneID;
673 timezone->getID(zoneID);
674 std::string result;
675 zoneID.toUTF8String(result);
676 return result;
677 }
678
GetDisplayName()679 std::string I18nTimeZone::GetDisplayName()
680 {
681 if (timezone == nullptr) {
682 return "";
683 }
684 std::string localeStr = LocaleConfig::GetSystemLocale();
685 return GetDisplayName(localeStr, false);
686 }
687
GetDisplayName(bool isDST)688 std::string I18nTimeZone::GetDisplayName(bool isDST)
689 {
690 std::string localeStr = LocaleConfig::GetSystemLocale();
691 return GetDisplayName(localeStr, isDST);
692 }
693
GetDisplayName(std::string localeStr)694 std::string I18nTimeZone::GetDisplayName(std::string localeStr)
695 {
696 return GetDisplayName(localeStr, false);
697 }
698
GetDisplayName(std::string localeStr,bool isDST)699 std::string I18nTimeZone::GetDisplayName(std::string localeStr, bool isDST)
700 {
701 icu::TimeZone::EDisplayType style = icu::TimeZone::EDisplayType::LONG_GENERIC;
702 icu::Locale locale(localeStr.data());
703 icu::UnicodeString name;
704 timezone->getDisplayName((UBool)isDST, style, locale, name);
705 std::string result;
706 name.toUTF8String(result);
707 return result;
708 }
709
ReadTimeZoneData(const char * xmlPath)710 bool I18nTimeZone::ReadTimeZoneData(const char *xmlPath)
711 {
712 xmlKeepBlanksDefault(0);
713 if (xmlPath == nullptr) {
714 return false;
715 }
716 xmlDocPtr doc = xmlParseFile(xmlPath);
717 if (!doc) {
718 return false;
719 }
720 xmlNodePtr cur = xmlDocGetRootElement(doc);
721 if (!cur || xmlStrcmp(cur->name, reinterpret_cast<const xmlChar *>(TIMEZONE_ROOT_TAG))) {
722 xmlFreeDoc(doc);
723 return false;
724 }
725 cur = cur->xmlChildrenNode;
726 while (cur != nullptr && !xmlStrcmp(cur->name, reinterpret_cast<const xmlChar *>(TIMEZONE_SECOND_ROOT_TAG))) {
727 xmlNodePtr value = cur->xmlChildrenNode;
728 xmlChar *contents[ELEMENT_NUM] = { 0 }; // 2 represent cityid, zoneid;
729 for (size_t i = 0; i < ELEMENT_NUM; i++) {
730 if (value != nullptr) {
731 contents[i] = xmlNodeGetContent(value);
732 value = value->next;
733 } else {
734 break;
735 }
736 }
737 // 0 represents cityid index, 1 represents zoneid index
738 availableZoneCityIDs.insert(reinterpret_cast<const char *>(contents[0]));
739 city2TimeZoneID.insert(
740 std::make_pair<std::string, std::string>(reinterpret_cast<const char *>(contents[0]),
741 reinterpret_cast<const char *>(contents[1])));
742 for (size_t i = 0; i < ELEMENT_NUM; i++) {
743 if (contents[i] != nullptr) {
744 xmlFree(contents[i]);
745 }
746 }
747 cur = cur->next;
748 }
749 xmlFreeDoc(doc);
750 return true;
751 }
752
GetTimezoneIDFromZoneInfo(std::set<std::string> & availableIDs,std::string & parentPath,std::string & parentName)753 void I18nTimeZone::GetTimezoneIDFromZoneInfo(std::set<std::string> &availableIDs, std::string &parentPath,
754 std::string &parentName)
755 {
756 using std::filesystem::directory_iterator;
757
758 struct stat s;
759 for (const auto &dirEntry : directory_iterator{parentPath}) {
760 std::string zonePath = dirEntry.path();
761 if (stat(zonePath.c_str(), &s) != 0) {
762 HiLog::Error(LABEL, "zoneinfo path %{public}s not exist.", parentPath.c_str());
763 return;
764 }
765 std::string zoneName = zonePath.substr(parentPath.length() + 1); // 1 add length of path splitor
766 std::string finalZoneName = parentName + "/" + zoneName;
767 if (s.st_mode & S_IFDIR) {
768 GetTimezoneIDFromZoneInfo(availableIDs, zonePath, finalZoneName);
769 } else {
770 availableIDs.insert(finalZoneName);
771 }
772 }
773 }
774
GetAvailableIDs(I18nErrorCode & errorCode)775 std::set<std::string> I18nTimeZone::GetAvailableIDs(I18nErrorCode &errorCode)
776 {
777 using std::filesystem::directory_iterator;
778
779 if (availableIDs.size() != 0) {
780 return availableIDs;
781 }
782 struct stat s;
783 for (const auto &dirEntry : directory_iterator{ZONEINFO_PATH}) {
784 std::string parentPath = dirEntry.path();
785 if (stat(parentPath.c_str(), &s) != 0) {
786 HiLog::Error(LABEL, "zoneinfo path %{public}s not exist.", parentPath.c_str());
787 errorCode = I18nErrorCode::FAILED;
788 return availableIDs;
789 }
790 std::string parentName = parentPath.substr(strlen(ZONEINFO_PATH) + 1);
791 if (s.st_mode & S_IFDIR) {
792 GetTimezoneIDFromZoneInfo(availableIDs, parentPath, parentName);
793 } else {
794 availableIDs.insert(parentName);
795 }
796 }
797 return availableIDs;
798 }
799
GetAvailableZoneCityIDs()800 std::set<std::string> I18nTimeZone::GetAvailableZoneCityIDs()
801 {
802 if (availableZoneCityIDs.size() != 0) {
803 return availableZoneCityIDs;
804 }
805 struct stat s;
806 if (stat(DEVICE_CITY_TIMEZONE_DATA_PATH, &s) == 0) {
807 ReadTimeZoneData(DEVICE_CITY_TIMEZONE_DATA_PATH);
808 } else {
809 ReadTimeZoneData(CITY_TIMEZONE_DATA_PATH);
810 }
811 return availableZoneCityIDs;
812 }
813
FindCityDisplayNameFromXml(std::string & cityID,std::string & locale)814 std::string I18nTimeZone::FindCityDisplayNameFromXml(std::string &cityID, std::string &locale)
815 {
816 xmlKeepBlanksDefault(0);
817 std::string xmlPath;
818 if (useDeviceCityDispName) {
819 xmlPath = DEVICE_CITY_DISPLAYNAME_PATH + locale + ".xml";
820 } else {
821 xmlPath = CITY_DISPLAYNAME_PATH + locale + ".xml";
822 }
823 xmlDocPtr doc = xmlParseFile(xmlPath.c_str());
824 if (!doc) {
825 HiLog::Error(LABEL, "can't parse city displayname file %{public}s", xmlPath.c_str());
826 return "";
827 }
828 xmlNodePtr cur = xmlDocGetRootElement(doc);
829 if (!cur || xmlStrcmp(cur->name, reinterpret_cast<const xmlChar *>(CITY_DISPLAYNAME_ROOT_TAG))) {
830 xmlFreeDoc(doc);
831 HiLog::Error(LABEL, "city displayname file %{public}s has wrong root tag.", xmlPath.c_str());
832 return "";
833 }
834 cur = cur->xmlChildrenNode;
835 std::string displayName;
836 while (cur != nullptr && !xmlStrcmp(cur->name,
837 reinterpret_cast<const xmlChar *>(CITY_DISPLAYNAME_SECOND_ROOT_TAG))) {
838 xmlNodePtr value = cur->xmlChildrenNode;
839 xmlChar *contents[ELEMENT_NUM] = { 0 }; // 2 represent cityid, displayName;
840 for (size_t i = 0; i < ELEMENT_NUM; i++) {
841 if (value != nullptr) {
842 contents[i] = xmlNodeGetContent(value);
843 value = value->next;
844 } else {
845 break;
846 }
847 }
848 if (strcmp(cityID.c_str(), reinterpret_cast<const char *>(contents[0])) == 0) {
849 displayName = reinterpret_cast<const char *>(contents[1]);
850 }
851 for (size_t i = 0; i < ELEMENT_NUM; i++) {
852 if (contents[i] != nullptr) {
853 xmlFree(contents[i]);
854 }
855 }
856 if (displayName.length() != 0) {
857 break;
858 }
859 cur = cur->next;
860 }
861 xmlFreeDoc(doc);
862 return displayName;
863 }
864
FindCityDisplayNameMap(std::string & locale)865 std::map<std::string, std::string> I18nTimeZone::FindCityDisplayNameMap(std::string &locale)
866 {
867 xmlKeepBlanksDefault(0);
868 std::map<std::string, std::string> resultMap;
869 std::string xmlPath;
870 if (useDeviceCityDispName) {
871 xmlPath = DEVICE_CITY_DISPLAYNAME_PATH + locale + ".xml";
872 } else {
873 xmlPath = CITY_DISPLAYNAME_PATH + locale + ".xml";
874 }
875 xmlDocPtr doc = xmlParseFile(xmlPath.c_str());
876 if (!doc) {
877 HiLog::Error(LABEL, "can't parse city displayname file %{public}s", xmlPath.c_str());
878 return resultMap;
879 }
880 xmlNodePtr cur = xmlDocGetRootElement(doc);
881 if (!cur || xmlStrcmp(cur->name, reinterpret_cast<const xmlChar *>(CITY_DISPLAYNAME_ROOT_TAG))) {
882 xmlFreeDoc(doc);
883 HiLog::Error(LABEL, "city displayname file %{public}s has wrong root tag.", xmlPath.c_str());
884 return resultMap;
885 }
886 cur = cur->xmlChildrenNode;
887 while (cur != nullptr && !xmlStrcmp(cur->name,
888 reinterpret_cast<const xmlChar *>(CITY_DISPLAYNAME_SECOND_ROOT_TAG))) {
889 xmlNodePtr value = cur->xmlChildrenNode;
890 xmlChar *contents[ELEMENT_NUM] = { 0 }; // 2 represent cityid, displayName;
891 for (size_t i = 0; i < ELEMENT_NUM; i++) {
892 if (value != nullptr) {
893 contents[i] = xmlNodeGetContent(value);
894 value = value->next;
895 } else {
896 break;
897 }
898 }
899 resultMap.insert(
900 std::make_pair<std::string, std::string>(reinterpret_cast<const char *>(contents[0]),
901 reinterpret_cast<const char *>(contents[1])));
902 for (size_t i = 0; i < ELEMENT_NUM; i++) {
903 if (contents[i] != nullptr) {
904 xmlFree(contents[i]);
905 }
906 }
907 cur = cur->next;
908 }
909 xmlFreeDoc(doc);
910 return resultMap;
911 }
912
GetSupportedLocales()913 bool I18nTimeZone::GetSupportedLocales()
914 {
915 using std::filesystem::directory_iterator;
916
917 struct stat s;
918 std::string displayNamePath;
919 if (stat(DEVICE_CITY_DISPLAYNAME_PATH, &s) == 0) {
920 useDeviceCityDispName = true;
921 displayNamePath = DEVICE_CITY_DISPLAYNAME_PATH;
922 } else {
923 displayNamePath = CITY_DISPLAYNAME_PATH;
924 }
925 for (const auto &dirEntry : directory_iterator{displayNamePath}) {
926 std::string xmlPath = dirEntry.path();
927 if (stat(xmlPath.c_str(), &s) != 0) {
928 HiLog::Error(LABEL, "city displayname file %{public}s not exist.", xmlPath.c_str());
929 return false;
930 }
931 int32_t localeStrLen = static_cast<int32_t>(xmlPath.length()) - static_cast<int32_t>(
932 displayNamePath.length()) - 4; // 4 is the length of ".xml"
933 std::string localeStr = xmlPath.substr(displayNamePath.length(), localeStrLen);
934 supportedLocales.insert(localeStr);
935 }
936 return true;
937 }
938
GetFallBack(std::string & localeStr)939 std::string I18nTimeZone::GetFallBack(std::string &localeStr)
940 {
941 if (strcmp(localeStr.c_str(), DEFAULT_LOCALE) == 0) {
942 return "";
943 }
944 size_t begin = 0;
945 std::vector<std::string> localeParts;
946 while (true) {
947 size_t end = localeStr.find('_', begin);
948 localeParts.push_back(localeStr.substr(begin, end - begin));
949 if (end == std::string::npos) {
950 break;
951 }
952 begin = end + 1;
953 }
954
955 if (localeParts.size() == 1) {
956 return DEFAULT_LOCALE;
957 }
958 std::string fallBackLocale;
959 for (size_t i = 0; i < localeParts.size() - 1; i++) {
960 fallBackLocale += localeParts[i];
961 if (i != localeParts.size() - 2) { // -2 represent the last part
962 fallBackLocale += "_";
963 }
964 }
965 return fallBackLocale;
966 }
967
GetCityDisplayName(std::string & cityID,std::string & localeStr)968 std::string I18nTimeZone::GetCityDisplayName(std::string &cityID, std::string &localeStr)
969 {
970 if (availableZoneCityIDs.size() == 0) {
971 GetAvailableZoneCityIDs();
972 }
973 if (availableZoneCityIDs.find(cityID) == availableZoneCityIDs.end()) {
974 HiLog::Error(LABEL, "%{public}s is not supported cityID.", cityID.c_str());
975 return "";
976 }
977 std::string requestLocaleStr = GetLocaleBaseName(localeStr);
978 if (requestLocaleStr.length() != 0) {
979 std::string displayName = FindCityDisplayNameFromXml(cityID, requestLocaleStr);
980 if (displayName.length() != 0) {
981 return displayName;
982 }
983 }
984 return "";
985 }
986
GetLocaleBaseName(std::string & localeStr)987 std::string I18nTimeZone::GetLocaleBaseName(std::string &localeStr)
988 {
989 if (supportedLocales.size() == 0) {
990 bool status = GetSupportedLocales();
991 if (!status) {
992 HiLog::Error(LABEL, "get supported Locales failed");
993 return "";
994 }
995 }
996 UErrorCode errorCode = U_ZERO_ERROR;
997 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, errorCode);
998 if (U_FAILURE(errorCode)) {
999 HiLog::Error(LABEL, "create icu Locale for %{public}s failed", localeStr.c_str());
1000 return "";
1001 }
1002 std::string requestLocaleStr = locale.getBaseName();
1003 std::string displayName;
1004 if (supportedLocales.find(requestLocaleStr) != supportedLocales.end()) {
1005 return requestLocaleStr;
1006 }
1007 locale.addLikelySubtags(errorCode);
1008 if (U_FAILURE(errorCode)) {
1009 HiLog::Error(LABEL, "add likely subtags for %{public}s failed", localeStr.c_str());
1010 return "";
1011 }
1012 requestLocaleStr = locale.getBaseName();
1013 while (requestLocaleStr.length() != 0) {
1014 if (supportedLocales.find(requestLocaleStr) != supportedLocales.end() || strcmp(requestLocaleStr.c_str(),
1015 DEFAULT_LOCALE) == 0) {
1016 return requestLocaleStr;
1017 }
1018 requestLocaleStr = GetFallBack(requestLocaleStr);
1019 }
1020 return displayName;
1021 }
1022
GetTimezoneIdByCityId(const std::string & cityId)1023 std::string I18nTimeZone::GetTimezoneIdByCityId(const std::string &cityId)
1024 {
1025 if (city2TimeZoneID.size() == 0) {
1026 GetAvailableZoneCityIDs();
1027 }
1028 if (city2TimeZoneID.find(cityId) == city2TimeZoneID.end()) {
1029 return "";
1030 } else {
1031 return city2TimeZoneID.at(cityId);
1032 }
1033 }
1034
GetTimezoneIdByLocation(const double x,const double y)1035 std::vector<std::string> I18nTimeZone::GetTimezoneIdByLocation(const double x, const double y)
1036 {
1037 std::vector<std::string> tzIdList;
1038 #ifdef SUPPORT_GRAPHICS
1039 std::map<int, std::string> categoryMap;
1040 int fixedX, fixedY;
1041 if (x < 0 && y >= 0) {
1042 categoryMap = categoryNum2TimezoneWN;
1043 } else if (x >= 0 && y >= 0) {
1044 categoryMap = categoryNum2TimezoneEN;
1045 } else if (x < 0 && y < 0) {
1046 categoryMap = categoryNum2TimezoneWS;
1047 } else {
1048 categoryMap = categoryNum2TimezoneES;
1049 }
1050 std::vector<std::string> filePaths = FindTzData();
1051 int pathLen = filePaths.size();
1052 if (pathLen < 1) {
1053 return tzIdList;
1054 }
1055 std::string preferredPath = GetPreferredPath(x, filePaths);
1056 if (preferredPath == "") {
1057 return tzIdList;
1058 }
1059 uint32_t width, height;
1060 GetTzDataWidth(filePaths, &width, &height);
1061 fixedX = (int)(y * (width / TZ_X_PLUS) + width / TZ_HALF_OF_SIZE);
1062 fixedY = (int)(x * ((height * pathLen) / (TZ_X_PLUS * TZ_HALF_OF_SIZE)) + (height * pathLen) / TZ_HALF_OF_SIZE);
1063 if (ParamExceedScope(fixedX, fixedY, width, height * pathLen)) {
1064 HiLog::Error(LABEL, "invalid width:%{public}d or height: %{public}d ", fixedX, fixedY);
1065 return tzIdList;
1066 }
1067 int actualHeight = pathLen > 1 ? (fixedY % height) : fixedY;
1068 std::vector<int> pixel = GetColorData(fixedX, fixedY, actualHeight, preferredPath);
1069 for (size_t i = 0; i < pixel.size(); i++) {
1070 //255 is invalid pixel value required
1071 if (pixel[i] != TZ_MAX_PIXEL_VALUE && categoryMap.find(pixel[i]) != categoryMap.end()) {
1072 std::string zdId = categoryMap[pixel[i]];
1073 tzIdList.push_back(zdId);
1074 }
1075 }
1076 #endif
1077 return tzIdList;
1078 }
1079
GetColorData(const int x,const int y,int actualHeight,std::string preferredPath)1080 std::vector<int> I18nTimeZone::GetColorData(const int x, const int y, int actualHeight, std::string preferredPath)
1081 {
1082 std::vector<int> result;
1083 FILE *fp;
1084 png_structp png_ptr;
1085 png_infop info_ptr;
1086 png_bytep row_pointers;
1087 int code = InitPngptr(png_ptr, info_ptr, &fp, preferredPath);
1088 if (code != 0) {
1089 return result;
1090 }
1091 try {
1092 rewind(fp);
1093 png_init_io(png_ptr, fp);
1094 png_read_info(png_ptr, info_ptr);
1095 unsigned int rowbytes = png_get_rowbytes(png_ptr, info_ptr);
1096 row_pointers = (png_bytep)png_malloc(png_ptr, rowbytes);
1097 if (row_pointers == nullptr) {
1098 png_destroy_read_struct(&png_ptr, &info_ptr, 0);
1099 CloseFile(fp);
1100 HiLog::Error(LABEL, "malloc rowbytes failed.");
1101 return result;
1102 }
1103 png_start_read_image(png_ptr);
1104 for (int i = 0; i < (actualHeight + 1); i++) {
1105 png_read_row(png_ptr, row_pointers, NULL);
1106 }
1107 for (size_t i = 0; i < 3; i++) { // 3 is RGB color pixel length
1108 std::string pixel = std::to_string(*(row_pointers + x * 3 + i)); // 3 is RGB color pixel length
1109 result.push_back(atoi(pixel.c_str()));
1110 }
1111 png_free(png_ptr, row_pointers);
1112 CloseFile(fp);
1113 } catch (...) {
1114 png_destroy_read_struct(&png_ptr, &info_ptr, 0);
1115 CloseFile(fp);
1116 }
1117 return result;
1118 }
1119
GetTzDataWidth(std::vector<std::string> filePaths,uint32_t * width,uint32_t * height)1120 void I18nTimeZone::GetTzDataWidth(std::vector<std::string> filePaths, uint32_t *width, uint32_t *height)
1121 {
1122 if (filePaths.size() == 0 || !CheckTzDataFilePath(filePaths.at(0))) {
1123 *width = 0;
1124 *height = 0;
1125 } else {
1126 FILE* fp;
1127 png_structp png_ptr;
1128 png_infop info_ptr;
1129 try {
1130 std::string path = filePaths.at(0);
1131 fp = fopen(path.c_str(), "rb");
1132 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1133 info_ptr = png_create_info_struct(png_ptr);
1134 rewind(fp);
1135 png_init_io(png_ptr, fp);
1136 png_read_info(png_ptr, info_ptr);
1137 unsigned int w, h;
1138 png_get_IHDR(png_ptr, info_ptr, &w, &h, NULL, NULL, NULL, NULL, NULL);
1139 *width = w;
1140 *height = h;
1141 png_destroy_read_struct(&png_ptr, &info_ptr, 0);
1142 CloseFile(fp);
1143 } catch(...) {
1144 png_destroy_read_struct(&png_ptr, &info_ptr, 0);
1145 CloseFile(fp);
1146 }
1147 }
1148 }
1149
InitPngptr(png_structp & png_ptr,png_infop & info_ptr,FILE ** fp,std::string preferredPath)1150 int I18nTimeZone::InitPngptr(png_structp &png_ptr, png_infop &info_ptr, FILE **fp,
1151 std::string preferredPath)
1152 {
1153 bool validFilePath = CheckTzDataFilePath(preferredPath);
1154 if (!validFilePath) {
1155 HiLog::Error(LABEL, "timezone data filepath invalid.");
1156 return 1;
1157 }
1158 *fp = fopen(preferredPath.c_str(), "rb");
1159 if (*fp == NULL) {
1160 HiLog::Error(LABEL, "timezone data resource file not exists.");
1161 return 1;
1162 }
1163 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1164 if (!png_ptr) {
1165 png_destroy_read_struct(&png_ptr, &info_ptr, 0);
1166 CloseFile(*fp);
1167 HiLog::Error(LABEL, "create read_struct failed.");
1168 return 1;
1169 }
1170 info_ptr = png_create_info_struct(png_ptr);
1171 if (!info_ptr) {
1172 png_destroy_read_struct(&png_ptr, &info_ptr, 0);
1173 CloseFile(*fp);
1174 HiLog::Error(LABEL, "create info_struct failed.");
1175 return 1;
1176 }
1177 return 0;
1178 }
1179
CheckTzDataFilePath(const std::string & filePath)1180 bool I18nTimeZone::CheckTzDataFilePath(const std::string &filePath)
1181 {
1182 char *realpathRes = NULL;
1183 realpathRes = realpath(filePath.c_str(), nullptr);
1184 if (realpathRes == NULL) {
1185 return false;
1186 }
1187 std::ifstream file(filePath.c_str());
1188 if (!file.good()) {
1189 file.close();
1190 free(realpathRes);
1191 return false;
1192 }
1193 file.close();
1194 free(realpathRes);
1195 realpathRes = NULL;
1196 return true;
1197 }
1198
GetPreferredPath(const double x,const std::vector<std::string> & filePaths)1199 std::string I18nTimeZone::GetPreferredPath(const double x,
1200 const std::vector<std::string> &filePaths)
1201 {
1202 if (filePaths.size() == 1) {
1203 return filePaths.at(0);
1204 } else {
1205 int fixedX = (int)(x + TZ_X_PLUS);
1206 for (unsigned int i = 0; i < filePaths.size(); i++) {
1207 std::string path = filePaths.at(i);
1208 std::string left = path.substr(path.find("-") + 1, 3);
1209 std::string right = path.substr(path.find("-") + 4, 3);
1210 if (fixedX >= atoi(left.c_str()) && fixedX < atoi(right.c_str())) {
1211 return path;
1212 }
1213 }
1214 return "";
1215 }
1216 }
1217
FindTzData()1218 std::vector<std::string> I18nTimeZone::FindTzData()
1219 {
1220 std::map<std::string, std::vector<std::string>> pathMap;
1221 std::regex reg("tz_[0-9]{7}-(\\d{6})\\.dat");
1222 std::regex regVersion("_[0-9]{7}");
1223 for (auto& entry : recursive_directory_iterator(TZ_PIXEL_PATH)) {
1224 const std::string path = entry.path().string();
1225 std::smatch match;
1226 bool found = std::regex_search(path, match, reg);
1227 if (found) {
1228 bool hasVerison = std::regex_search(path, match, regVersion);
1229 if (hasVerison) {
1230 std::string version = match[0].str();
1231 SetVersionPathMap(version, path, &pathMap);
1232 }
1233 }
1234 }
1235 std::vector<std::string> filePaths;
1236 std::map<std::string, std::vector<std::string>>::reverse_iterator iter;
1237 for (iter = pathMap.rbegin(); iter != pathMap.rend(); ++iter) {
1238 if (ValidateDataIntegrity(iter->second)) {
1239 return iter->second;
1240 }
1241 }
1242 return filePaths;
1243 }
1244
ValidateDataIntegrity(const std::vector<std::string> & pathList)1245 bool I18nTimeZone::ValidateDataIntegrity(const std::vector<std::string> &pathList)
1246 {
1247 size_t total = 0;
1248 for (unsigned int i = 0; i < pathList.size(); i++) {
1249 std::string path = pathList.at(i);
1250 std::string left = path.substr(path.find("-") + 1, 3);
1251 std::string right = path.substr(path.find("-") + 4, 3);
1252 total += abs(atoi(right.c_str()) - atoi(left.c_str()));
1253 }
1254 return total == TZ_X_PLUS * TZ_HALF_OF_SIZE;
1255 }
1256
SetVersionPathMap(std::string verison,std::string path,std::map<std::string,std::vector<std::string>> * pathMap)1257 void I18nTimeZone::SetVersionPathMap(std::string verison, std::string path,
1258 std::map<std::string, std::vector<std::string>> *pathMap)
1259 {
1260 if (pathMap->find(verison) != pathMap->end()) {
1261 std::vector<std::string> *list = &(pathMap->find(verison)->second);
1262 list->push_back(path);
1263 } else {
1264 std::vector<std::string> list;
1265 list.push_back(path);
1266 pathMap->insert(std::pair<std::string, std::vector<std::string>>(verison, list));
1267 }
1268 }
1269
CloseFile(FILE * fp)1270 void I18nTimeZone::CloseFile(FILE *fp)
1271 {
1272 if (fp != nullptr && fp != NULL) {
1273 fclose(fp);
1274 }
1275 }
1276
ParamExceedScope(const int x,const int y,int fixedX,int fixedY)1277 bool I18nTimeZone::ParamExceedScope(const int x, const int y, int fixedX, int fixedY)
1278 {
1279 if (x < 0 || y < 0 || fixedX == 0 || fixedY == 0) {
1280 return true;
1281 }
1282 if (x > (fixedX - 1) || y > (fixedY - 1)) {
1283 return true;
1284 }
1285 return false;
1286 }
1287 }
1288 }
1289 }
1290