• 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 "character.h"
16 #include "error_util.h"
17 #include "i18n_hilog.h"
18 #include "i18n_timezone_addon.h"
19 #include "js_utils.h"
20 #include "variable_convertor.h"
21 
22 namespace OHOS {
23 namespace Global {
24 namespace I18n {
25 static thread_local napi_ref* g_timezoneConstructor = nullptr;
26 
I18nTimeZoneAddon()27 I18nTimeZoneAddon::I18nTimeZoneAddon() {}
28 
~I18nTimeZoneAddon()29 I18nTimeZoneAddon::~I18nTimeZoneAddon() {}
30 
Destructor(napi_env env,void * nativeObject,void * hint)31 void I18nTimeZoneAddon::Destructor(napi_env env, void *nativeObject, void *hint)
32 {
33     if (!nativeObject) {
34         return;
35     }
36     delete reinterpret_cast<I18nTimeZoneAddon *>(nativeObject);
37     nativeObject = nullptr;
38 }
39 
GetI18nTimeZone(napi_env env,napi_callback_info info)40 napi_value I18nTimeZoneAddon::GetI18nTimeZone(napi_env env, napi_callback_info info)
41 {
42     size_t argc = 1;
43     napi_value argv[1] = { nullptr };
44     napi_value thisVar = nullptr;
45     void *data = nullptr;
46     napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
47     if (!VariableConvertor::CheckNapiIsNull(env, argv[0])) {
48         napi_status status = napi_create_string_utf8(env, "", NAPI_AUTO_LENGTH, &argv[0]);
49         if (status != napi_ok) {
50             HILOG_ERROR_I18N("I18nTimeZoneAddon::GetI18nTimeZone: create string failed.");
51             return nullptr;
52         }
53     }
54     return StaticGetTimeZone(env, argv, true);
55 }
56 
InitI18nTimeZone(napi_env env,napi_value exports)57 napi_value I18nTimeZoneAddon::InitI18nTimeZone(napi_env env, napi_value exports)
58 {
59     napi_property_descriptor properties[] = {
60         DECLARE_NAPI_FUNCTION("getID", GetID),
61         DECLARE_NAPI_FUNCTION("getDisplayName", GetTimeZoneDisplayName),
62         DECLARE_NAPI_FUNCTION("getRawOffset", GetRawOffset),
63         DECLARE_NAPI_FUNCTION("getOffset", GetOffset),
64     };
65     napi_value constructor = nullptr;
66     napi_status status = napi_define_class(env, "TimeZone", NAPI_AUTO_LENGTH, I18nTimeZoneConstructor, nullptr,
67         sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor);
68     if (status != napi_ok) {
69         HILOG_ERROR_I18N("InitI18nTimeZone: Failed to define class TimeZone at Init");
70         return nullptr;
71     }
72     exports = I18nTimeZoneAddon::InitTimeZone(env, exports);
73     g_timezoneConstructor = new (std::nothrow) napi_ref;
74     if (!g_timezoneConstructor) {
75         HILOG_ERROR_I18N("InitI18nTimeZone: Failed to create TimeZone ref at init");
76         return nullptr;
77     }
78     status = napi_create_reference(env, constructor, 1, g_timezoneConstructor);
79     if (status != napi_ok) {
80         HILOG_ERROR_I18N("InitI18nTimeZone: Failed to create reference g_timezoneConstructor at init");
81         return nullptr;
82     }
83     return exports;
84 }
85 
InitTimeZone(napi_env env,napi_value exports)86 napi_value I18nTimeZoneAddon::InitTimeZone(napi_env env, napi_value exports)
87 {
88     napi_property_descriptor properties[] = {
89         DECLARE_NAPI_STATIC_FUNCTION("getAvailableIDs", GetAvailableTimezoneIDs),
90         DECLARE_NAPI_STATIC_FUNCTION("getAvailableZoneCityIDs", GetAvailableZoneCityIDs),
91         DECLARE_NAPI_STATIC_FUNCTION("getCityDisplayName", GetCityDisplayName),
92         DECLARE_NAPI_STATIC_FUNCTION("getTimezoneFromCity", GetTimezoneFromCity),
93         DECLARE_NAPI_STATIC_FUNCTION("getTimezonesByLocation", GetTimezonesByLocation)
94     };
95     napi_value constructor = nullptr;
96     napi_status status = napi_define_class(env, "I18nTimeZone", NAPI_AUTO_LENGTH, JSUtils::DefaultConstructor, nullptr,
97         sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor);
98     if (status != napi_ok) {
99         HILOG_ERROR_I18N("InitTimeZone: Failed to define class TimeZone.");
100         return nullptr;
101     }
102     status = napi_set_named_property(env, exports, "TimeZone", constructor);
103     if (status != napi_ok) {
104         HILOG_ERROR_I18N("InitTimeZone: Set property failed When InitTimeZone.");
105         return nullptr;
106     }
107     return exports;
108 }
109 
I18nTimeZoneConstructor(napi_env env,napi_callback_info info)110 napi_value I18nTimeZoneAddon::I18nTimeZoneConstructor(napi_env env, napi_callback_info info)
111 {
112     size_t argc = 2;
113     napi_value argv[2] = { nullptr };
114     napi_value thisVar = nullptr;
115     void *data = nullptr;
116     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
117     if (status != napi_ok) {
118         return nullptr;
119     }
120     std::string zoneID = "";
121     napi_valuetype valueType = napi_valuetype::napi_undefined;
122     if (argc > 0) {
123         napi_typeof(env, argv[0], &valueType);
124         if (valueType != napi_valuetype::napi_string) {
125             return nullptr;
126         }
127         int32_t code = 0;
128         zoneID = VariableConvertor::GetString(env, argv[0], code);
129         if (code != 0) {
130             return nullptr;
131         }
132     }
133     if (argc < FUNC_ARGS_COUNT) {
134         return nullptr;
135     }
136     napi_typeof(env, argv[1], &valueType);
137     if (valueType != napi_valuetype::napi_boolean) {
138         return nullptr;
139     }
140     bool isZoneID = false;
141     status = napi_get_value_bool(env, argv[1], &isZoneID);
142     if (status != napi_ok) {
143         return nullptr;
144     }
145     std::unique_ptr<I18nTimeZoneAddon> obj = std::make_unique<I18nTimeZoneAddon>();
146     status =
147         napi_wrap(env, thisVar, reinterpret_cast<void *>(obj.get()), I18nTimeZoneAddon::Destructor, nullptr, nullptr);
148     if (status != napi_ok) {
149         return nullptr;
150     }
151     obj->timezone_ = I18nTimeZone::CreateInstance(zoneID, isZoneID);
152     if (!obj->timezone_) {
153         return nullptr;
154     }
155     obj.release();
156     return thisVar;
157 }
158 
GetAvailableTimezoneIDs(napi_env env,napi_callback_info info)159 napi_value I18nTimeZoneAddon::GetAvailableTimezoneIDs(napi_env env, napi_callback_info info)
160 {
161     I18nErrorCode errorCode = I18nErrorCode::SUCCESS;
162     std::set<std::string> timezoneIDs = I18nTimeZone::GetAvailableIDs(errorCode);
163     if (errorCode != I18nErrorCode::SUCCESS) {
164         return nullptr;
165     }
166     napi_value result = nullptr;
167     napi_status status = napi_create_array_with_length(env, timezoneIDs.size(), &result);
168     if (status != napi_ok) {
169         HILOG_ERROR_I18N("GetAvailableTimezoneIDs: Failed to create array");
170         return nullptr;
171     }
172     size_t index = 0;
173     for (std::set<std::string>::iterator it = timezoneIDs.begin(); it != timezoneIDs.end(); ++it) {
174         napi_value value = nullptr;
175         status = napi_create_string_utf8(env, (*it).c_str(), NAPI_AUTO_LENGTH, &value);
176         if (status != napi_ok) {
177             HILOG_ERROR_I18N("Failed to create string item");
178             return nullptr;
179         }
180         status = napi_set_element(env, result, index, value);
181         if (status != napi_ok) {
182             HILOG_ERROR_I18N("Failed to set array item");
183             return nullptr;
184         }
185         ++index;
186     }
187     return result;
188 }
189 
GetAvailableZoneCityIDs(napi_env env,napi_callback_info info)190 napi_value I18nTimeZoneAddon::GetAvailableZoneCityIDs(napi_env env, napi_callback_info info)
191 {
192     std::set<std::string> cityIDs = I18nTimeZone::GetAvailableZoneCityIDs();
193     napi_value result = nullptr;
194     napi_status status = napi_create_array_with_length(env, cityIDs.size(), &result);
195     if (status != napi_ok) {
196         HILOG_ERROR_I18N("GetAvailableZoneCityIDs: Failed to create array");
197         return nullptr;
198     }
199     size_t index = 0;
200     for (auto it = cityIDs.begin(); it != cityIDs.end(); ++it) {
201         napi_value value = nullptr;
202         status = napi_create_string_utf8(env, (*it).c_str(), NAPI_AUTO_LENGTH, &value);
203         if (status != napi_ok) {
204             HILOG_ERROR_I18N("GetAvailableZoneCityIDs: Failed to create string item");
205             return nullptr;
206         }
207         status = napi_set_element(env, result, index, value);
208         if (status != napi_ok) {
209             HILOG_ERROR_I18N("GetAvailableZoneCityIDs: Failed to set array item");
210             return nullptr;
211         }
212         ++index;
213     }
214     return result;
215 }
216 
GetCityDisplayName(napi_env env,napi_callback_info info)217 napi_value I18nTimeZoneAddon::GetCityDisplayName(napi_env env, napi_callback_info info)
218 {
219     size_t argc = 2;
220     napi_value argv[2] = { 0 };
221     napi_value thisVar = nullptr;
222     void *data = nullptr;
223     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
224     if (status != napi_ok) {
225         return nullptr;
226     }
227     if (argc < FUNC_ARGS_COUNT) {
228         return nullptr;
229     }
230     napi_valuetype valueType = napi_valuetype::napi_undefined;
231     napi_typeof(env, argv[0], &valueType);
232     if (valueType != napi_valuetype::napi_string) {
233         HILOG_ERROR_I18N("GetCityDisplayName: Invalid parameter type");
234         return nullptr;
235     }
236     int32_t code = 0;
237     std::string cityID = VariableConvertor::GetString(env, argv[0], code);
238     if (code != 0) {
239         return nullptr;
240     }
241     std::string locale = VariableConvertor::GetString(env, argv[1], code);
242     if (code != 0) {
243         return nullptr;
244     }
245     std::string name = I18nTimeZone::GetCityDisplayName(cityID, locale);
246     napi_value result = nullptr;
247     status = napi_create_string_utf8(env, name.c_str(), NAPI_AUTO_LENGTH, &result);
248     if (status != napi_ok) {
249         return nullptr;
250     }
251     return result;
252 }
253 
GetTimezoneFromCity(napi_env env,napi_callback_info info)254 napi_value I18nTimeZoneAddon::GetTimezoneFromCity(napi_env env, napi_callback_info info)
255 {
256     size_t argc = 1;
257     napi_value argv[1] = { nullptr };
258     napi_value thisVar = nullptr;
259     void *data = nullptr;
260     napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
261     return StaticGetTimeZone(env, argv, false);
262 }
263 
GetTimezonesByLocation(napi_env env,napi_callback_info info)264 napi_value I18nTimeZoneAddon::GetTimezonesByLocation(napi_env env, napi_callback_info info)
265 {
266     size_t argc = 2;
267     napi_value argv[2] = {0, 0};
268     napi_value thisVar = nullptr;
269     void *data = nullptr;
270     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
271     if (status != napi_ok) {
272         return nullptr;
273     }
274     if (argc < FUNC_ARGS_COUNT) {
275         HILOG_ERROR_I18N("GetTimezonesByLocation: Missing parameter");
276         ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, "longitude or latitude", "", true);
277         return nullptr;
278     }
279     double x;
280     double y;
281     VariableConvertor::VerifyType(env, "longitude", "number", argv[0]);
282     VariableConvertor::VerifyType(env, "latitude", "number", argv[1]);
283     if (!CheckLongitudeTypeAndScope(env, argv[0], x) ||
284         !CheckLatitudeTypeAndScope(env, argv[1], y)) {
285         ErrorUtil::NapiThrow(env, I18N_NOT_VALID, "longitude or latitude", "a valid value", true);
286         return nullptr;
287     }
288     napi_value timezoneList = nullptr;
289     napi_create_array(env, &timezoneList);
290     std::vector<std::string> tempList = I18nTimeZone::GetTimezoneIdByLocation(x, y);
291     for (size_t i = 0; i < tempList.size(); i++) {
292         napi_value timezoneId = nullptr;
293         status = napi_create_string_utf8(env, tempList[i].c_str(), NAPI_AUTO_LENGTH, &timezoneId);
294         if (status != napi_ok) {
295             return nullptr;
296         }
297         napi_value argTimeZoneId[1] = { timezoneId };
298         napi_value timezone = StaticGetTimeZone(env, argTimeZoneId, true);
299         status = napi_set_element(env, timezoneList, i, timezone);
300         if (status != napi_ok) {
301             return nullptr;
302         }
303     }
304 
305     return timezoneList;
306 }
307 
CheckLongitudeTypeAndScope(napi_env env,napi_value argv,double & x)308 bool I18nTimeZoneAddon::CheckLongitudeTypeAndScope(napi_env env, napi_value argv, double &x)
309 {
310     napi_status status = napi_get_value_double(env, argv, &x);
311     if (status != napi_ok) {
312         HILOG_ERROR_I18N("GetTimezonesByLocation: Parse first argument x failed");
313         return false;
314     }
315     // -180 and 179.9 is the scope of longitude
316     if (x < -180 || x > 179.9) {
317         HILOG_ERROR_I18N("GetTimezonesByLocation: Args x exceed it's scope.");
318         return false;
319     }
320     return true;
321 }
322 
CheckLatitudeTypeAndScope(napi_env env,napi_value argv,double & y)323 bool I18nTimeZoneAddon::CheckLatitudeTypeAndScope(napi_env env, napi_value argv, double &y)
324 {
325     napi_status status = napi_get_value_double(env, argv, &y);
326     if (status != napi_ok) {
327         HILOG_ERROR_I18N("GetTimezonesByLocation: Parse second argument y failed");
328         return false;
329     }
330     // -90 and 89.9 is the scope of latitude
331     if (y < -90 || y > 89.9) {
332         HILOG_ERROR_I18N("GetTimezonesByLocation: Args y exceed it's scope.");
333         return false;
334     }
335     return true;
336 }
337 
GetID(napi_env env,napi_callback_info info)338 napi_value I18nTimeZoneAddon::GetID(napi_env env, napi_callback_info info)
339 {
340     size_t argc = 0;
341     napi_value *argv = nullptr;
342     napi_value thisVar = nullptr;
343     void *data = nullptr;
344     napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
345     I18nTimeZoneAddon *obj = nullptr;
346     napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
347     if (status != napi_ok || !obj || !obj->timezone_) {
348         HILOG_ERROR_I18N("GetID: Get TimeZone object failed");
349         return nullptr;
350     }
351     std::string result = obj->timezone_->GetID();
352     napi_value value = nullptr;
353     status = napi_create_string_utf8(env, result.c_str(), NAPI_AUTO_LENGTH, &value);
354     if (status != napi_ok) {
355         HILOG_ERROR_I18N("GetID: Create result failed");
356         return nullptr;
357     }
358     return value;
359 }
360 
GetTimeZoneDisplayName(napi_env env,napi_callback_info info)361 napi_value I18nTimeZoneAddon::GetTimeZoneDisplayName(napi_env env, napi_callback_info info)
362 {
363     size_t argc = 2;
364     napi_value argv[2] = { 0 };
365     napi_value thisVar = nullptr;
366     void *data = nullptr;
367     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
368     if (status != napi_ok) {
369         return nullptr;
370     }
371 
372     I18nTimeZoneAddon *obj = nullptr;
373     status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
374     if (status != napi_ok || !obj || !obj->timezone_) {
375         HILOG_ERROR_I18N("GetTimeZoneDisplayName: Get TimeZone object failed");
376         return nullptr;
377     }
378 
379     std::string locale;
380     bool isDST = false;
381     int32_t parameterStatus = GetParameter(env, argv, locale, isDST);
382 
383     std::string result;
384     if (parameterStatus == -1) {  // -1 represents Invalid parameter.
385         HILOG_ERROR_I18N("GetTimeZoneDisplayName: Parameter type does not match");
386         return nullptr;
387     } else if (parameterStatus == 0) {
388         result = obj->timezone_->GetDisplayName();
389     } else if (parameterStatus == 1) {  // 1 represents one string parameter.
390         result = obj->timezone_->GetDisplayName(locale);
391     } else if (parameterStatus == 2) {  // 2 represents one boolean parameter.
392         result = obj->timezone_->GetDisplayName(isDST);
393     } else {
394         result = obj->timezone_->GetDisplayName(locale, isDST);
395     }
396 
397     napi_value value = nullptr;
398     status = napi_create_string_utf8(env, result.c_str(), NAPI_AUTO_LENGTH, &value);
399     if (status != napi_ok) {
400         HILOG_ERROR_I18N("GetTimeZoneDisplayName: Create result failed");
401         return nullptr;
402     }
403     return value;
404 }
405 
GetRawOffset(napi_env env,napi_callback_info info)406 napi_value I18nTimeZoneAddon::GetRawOffset(napi_env env, napi_callback_info info)
407 {
408     size_t argc = 0;
409     napi_value *argv = nullptr;
410     napi_value thisVar = nullptr;
411     void *data = nullptr;
412     napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
413     I18nTimeZoneAddon *obj = nullptr;
414     napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
415     if (status != napi_ok || !obj || !obj->timezone_) {
416         HILOG_ERROR_I18N("GetRawOffset: Get TimeZone object failed");
417         return nullptr;
418     }
419     int32_t result = obj->timezone_->GetRawOffset();
420     napi_value value = nullptr;
421     status = napi_create_int32(env, result, &value);
422     if (status != napi_ok) {
423         HILOG_ERROR_I18N("GetRawOffset: Create result failed");
424         return nullptr;
425     }
426     return value;
427 }
428 
GetOffset(napi_env env,napi_callback_info info)429 napi_value I18nTimeZoneAddon::GetOffset(napi_env env, napi_callback_info info)
430 {
431     size_t argc = 1;
432     napi_value argv[1] = { 0 };
433     napi_value thisVar = nullptr;
434     void *data = nullptr;
435     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
436     if (status != napi_ok) {
437         return nullptr;
438     }
439 
440     double date = 0;
441     if (VariableConvertor::CheckNapiIsNull(env, argv[0])) {
442         napi_valuetype valueType = napi_valuetype::napi_undefined;
443         napi_typeof(env, argv[0], &valueType);
444         if (valueType != napi_valuetype::napi_number) {
445             HILOG_ERROR_I18N("GetOffset: Invalid parameter type");
446             return nullptr;
447         }
448         status = napi_get_value_double(env, argv[0], &date);
449         if (status != napi_ok) {
450             HILOG_ERROR_I18N("Get parameter date failed");
451             return nullptr;
452         }
453     } else {
454         auto time = std::chrono::system_clock::now();
455         auto since_epoch = time.time_since_epoch();
456         auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(since_epoch);
457         date = (double)millis.count();
458     }
459 
460     I18nTimeZoneAddon *obj = nullptr;
461     status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
462     if (status != napi_ok || !obj || !obj->timezone_) {
463         HILOG_ERROR_I18N("GetOffset: Get TimeZone object failed");
464         return nullptr;
465     }
466     int32_t result = obj->timezone_->GetOffset(date);
467     napi_value value = nullptr;
468     status = napi_create_int32(env, result, &value);
469     if (status != napi_ok) {
470         HILOG_ERROR_I18N("GetOffset: Create result failed");
471         return nullptr;
472     }
473     return value;
474 }
475 
StaticGetTimeZone(napi_env env,napi_value * argv,bool isZoneID)476 napi_value I18nTimeZoneAddon::StaticGetTimeZone(napi_env env, napi_value *argv, bool isZoneID)
477 {
478     napi_value constructor = nullptr;
479     napi_status status = napi_get_reference_value(env, *g_timezoneConstructor, &constructor);
480     if (status != napi_ok) {
481         HILOG_ERROR_I18N("Failed to create reference at StaticGetTimeZone");
482         return nullptr;
483     }
484     napi_value newArgv[2] = { 0 };
485     newArgv[0] = argv[0];
486     status = napi_get_boolean(env, isZoneID, &newArgv[1]);
487     if (status != napi_ok) {
488         return nullptr;
489     }
490     napi_value result = nullptr;
491     status = napi_new_instance(env, constructor, 2, newArgv, &result); // 2 is parameter num
492     if (status != napi_ok) {
493         HILOG_ERROR_I18N("StaticGetTimeZone create instance failed");
494         return nullptr;
495     }
496     return result;
497 }
498 
GetParameter(napi_env env,napi_value * argv,std::string & localeStr,bool & isDST)499 int32_t I18nTimeZoneAddon::GetParameter(napi_env env, napi_value *argv, std::string &localeStr, bool &isDST)
500 {
501     napi_status status = napi_ok;
502     if (VariableConvertor::CheckNapiIsNull(env, argv[1])) {
503         napi_valuetype valueType0 = napi_valuetype::napi_undefined;
504         napi_valuetype valueType1 = napi_valuetype::napi_undefined;
505         napi_typeof(env, argv[0], &valueType0);  // 0 represents first parameter
506         napi_typeof(env, argv[1], &valueType1);  // 1 represents second parameter
507         bool firstParamFlag = VariableConvertor::CheckNapiIsNull(env, argv[0]);
508         if (valueType1 == napi_valuetype::napi_boolean) {
509             status = napi_get_value_bool(env, argv[1], &isDST);
510             if (status != napi_ok) {
511                 return -1;  // -1 represents Invalid parameter.
512             } else if (!firstParamFlag) {
513                 return 2;  // 2 represents one boolean parameter.
514             }
515             if (valueType0 == napi_valuetype::napi_string &&
516                  GetStringFromJS(env, argv[0], localeStr)) {
517                 return 3;  // 3 represents one string parameter and one bool parameter.
518             }
519         }
520         return -1;  // -1 represents Invalid parameter.
521     }
522     return GetFirstParameter(env, argv[0], localeStr, isDST);
523 }
524 
GetStringFromJS(napi_env env,napi_value argv,std::string & jsString)525 bool I18nTimeZoneAddon::GetStringFromJS(napi_env env, napi_value argv, std::string &jsString)
526 {
527     size_t len = 0;
528     napi_status status = napi_get_value_string_utf8(env, argv, nullptr, 0, &len);
529     if (status != napi_ok) {
530         HILOG_ERROR_I18N("Failed to get string length");
531         return false;
532     }
533     std::vector<char> argvBuf(len + 1);
534     status = napi_get_value_string_utf8(env, argv, argvBuf.data(), len + 1, &len);
535     if (status != napi_ok) {
536         HILOG_ERROR_I18N("Failed to get string item");
537         return false;
538     }
539     jsString = argvBuf.data();
540     return true;
541 }
542 
GetFirstParameter(napi_env env,napi_value value,std::string & localeStr,bool & isDST)543 int32_t I18nTimeZoneAddon::GetFirstParameter(napi_env env, napi_value value, std::string &localeStr, bool &isDST)
544 {
545     if (!VariableConvertor::CheckNapiIsNull(env, value)) {
546         return 0;  // 0 represents no parameter.
547     } else {
548         napi_status status = napi_ok;
549         napi_valuetype valueType = napi_valuetype::napi_undefined;
550         napi_typeof(env, value, &valueType);
551         if (valueType == napi_valuetype::napi_string) {
552             bool valid = GetStringFromJS(env, value, localeStr);
553             // -1 represents Invalid parameter.
554             // 1 represents one string parameter.
555             return !valid ? -1 : 1;
556         } else if (valueType == napi_valuetype::napi_boolean) {
557             status = napi_get_value_bool(env, value, &isDST);
558             // -1 represents Invalid parameter.
559             // 2 represents one boolean parameter.
560             return (status != napi_ok) ? -1 : 2;
561         }
562         return -1;  // -1 represents Invalid parameter.
563     }
564 }
565 } // namespace I18n
566 } // namespace Global
567 } // namespace OHOS