1 /*
2 * Copyright (c) 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
16 #include "common/napi_helper.cpp"
17 #include "common/native_common.h"
18 #include "napi/native_api.h"
19 #include <cerrno>
20 #include <climits>
21 #include <clocale>
22 #include <ctime>
23 #include <ifaddrs.h>
24 #include <js_native_api_types.h>
25 #include <net/if.h>
26
27 #define MAX_NAMBER 80
28 #define FAIL (-1)
29 #define PARAM_0 0
30 #define PARAM_1 1
31 #define INIT (-1)
32 #define SUCCESS 0
33 #define ERRON_0 0
34 #define ONE_YEAR 31622400LL
35
36 extern "C" struct tm *__localtime64(const time_t *);
Localtime64(napi_env env, napi_callback_info info)37 static napi_value Localtime64(napi_env env, napi_callback_info info)
38 {
39 int ret = SUCCESS;
40 struct tm *tm;
41 const time_t time = INT_MIN * ONE_YEAR + PARAM_1;
42 errno = ERRON_0;
43 tm = __localtime64(&time);
44 if (tm == nullptr || errno == EOVERFLOW) {
45 ret = FAIL;
46 }
47 napi_value result = nullptr;
48 napi_create_int32(env, ret, &result);
49 return result;
50 }
51
52 EXTERN_C_START
Init(napi_env env, napi_value exports)53 static napi_value Init(napi_env env, napi_value exports)
54 {
55 napi_property_descriptor desc[] = {
56 {"__localtime64", nullptr, Localtime64, nullptr, nullptr, nullptr, napi_default, nullptr},
57 };
58 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
59 return exports;
60 }
61
62 EXTERN_C_END
63
64 static napi_module demoModule = {
65 .nm_version = 1,
66 .nm_flags = 0,
67 .nm_filename = nullptr,
68 .nm_register_func = Init,
69 .nm_modname = "locale1",
70 .nm_priv = ((void *)0),
71 .reserved = {0},
72 };
73
RegisterEntryModule(void)74 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
75