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 <clocale>
21 #include <csignal>
22 #include <cstdio>
23 #include <cstdlib>
24 #include <cstring>
25 #include <ctime>
26 #include <ifaddrs.h>
27 #include <js_native_api.h>
28 #include <js_native_api_types.h>
29 #include <linux/quota.h>
30 #include <malloc.h>
31 #include <net/if.h>
32 #include <node_api.h>
33 #include <sched.h>
34 #include <string>
35 #include <sys/inotify.h>
36 #include <sys/time.h>
37 #include <sys/timex.h>
38 #include <unistd.h>
39 #include <utmp.h>
40 #include <uv.h>
41 #include <securec.h>
42
43 #define ONEVAL 1
44 #define TWOVAL 2
45 #define THREE 3
46 #define NO_ERR 0
47 #define SUCCESS 1
48 #define FAIL (-1)
49 #define TEST_SIZE 2
50 #define ONE 1
51 #define PARAM_4 4
52 #define MAX_NUMBER 256
53 #define FAILNUMBER (-1)
54 #define TIME_L 1659177614
55 #define MAX_UTIME (3)
56 static int g_count = 0;
57 #define SIGNUM 40
58 #define TVSEC 1000000000
59 #define TVNSEC 200000000
60
61 #define PARAM_0 0
62 #define PARAM_1 1
63 #define PARAM_2 2
64 #define PARAM_5 5
65 #define PARAM_1999 1999
66 #define PARAM_12 12
67 #define PARAM_17 17
68 #define PARAM_06 06
69 #define PARAM_30 30
70 #define PARAM_23 23
71 #define PARAM_1900 1900
72 #define PARAM_02 02
73 #define PARAM_07 07
74 #define PARAM_16 16
75 #define PARAM_15 15
76 #define PARAM_20 20
77 #define PARAM_UNNORMAL (-1)
78 #define RETURN_0 0
79 #define FAILD (-1)
80 #define ERRON_0 0
81 #define SIZE_10 10
82 #define SIZE_30 30
83 #define SIZE_64 64
84 #define SIZE_100 100
85 #define SIZE_1024 1024
86 #define SIZE_4096 4096
87 #define SIZE_8192 8192
88 #define SIZE_0666 0666
GetITimer(napi_env env,napi_callback_info info)89 static napi_value GetITimer(napi_env env, napi_callback_info info)
90 {
91 struct itimerval currValue;
92 int getInfo = getitimer(ITIMER_VIRTUAL, &currValue);
93 napi_value result = nullptr;
94 napi_create_int32(env, getInfo, &result);
95 return result;
96 }
GetTimeOfDay(napi_env env,napi_callback_info info)97 static napi_value GetTimeOfDay(napi_env env, napi_callback_info info)
98 {
99 errno = NO_ERR;
100 struct timeval start;
101 int gitInfo = gettimeofday(&start, nullptr);
102 napi_value result = nullptr;
103 napi_create_int32(env, gitInfo, &result);
104 return result;
105 }
106
signalHandler(int signo)107 void signalHandler(int signo)
108 {
109 switch (signo) {
110 case SIGALRM:
111 printf("Caught the SIGALRM signal!\n");
112 break;
113 default:
114 printf("Can not catch the SIGALRM signal!\n");
115 break;
116 }
117 }
118
Setitimer(napi_env env,napi_callback_info info)119 static napi_value Setitimer(napi_env env, napi_callback_info info)
120 {
121 size_t argc = PARAM_4;
122 napi_value args[4] = {nullptr};
123 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
124 int valueFirst;
125 napi_get_value_int32(env, args[0], &valueFirst);
126 int valueSecond;
127 napi_get_value_int32(env, args[1], &valueSecond);
128 int valueThird;
129 napi_get_value_int32(env, args[2], &valueThird);
130 int valueForth;
131 napi_get_value_int32(env, args[3], &valueForth);
132 signal(SIGALRM, signalHandler);
133 struct itimerval it;
134 memset(&it, PARAM_0, sizeof(it));
135 it.it_value.tv_sec = valueFirst;
136 it.it_value.tv_usec = valueSecond;
137 it.it_interval.tv_sec = valueThird;
138 it.it_interval.tv_usec = valueForth;
139 int setValue = setitimer(ITIMER_REAL, &it, nullptr);
140 napi_value result;
141 napi_create_int32(env, setValue, &result);
142 return result;
143 }
144
Strftime(napi_env env,napi_callback_info info)145 static napi_value Strftime(napi_env env, napi_callback_info info)
146 {
147 size_t argc = PARAM_1;
148 napi_value args[1] = {nullptr};
149 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
150 char *valueFirst = NapiHelper::GetString(env, args[0]);
151 tzset();
152 static time_t gTime = TIME_L;
153 struct tm *timeptr = localtime(&gTime);
154 char buffer[MAX_NUMBER];
155 size_t ret = strftime(buffer, sizeof(buffer) - PARAM_1, valueFirst, timeptr);
156 napi_value result = nullptr;
157 if (ret != 0) {
158 napi_create_string_utf8(env, buffer, NAPI_AUTO_LENGTH, &result);
159 }
160 return result;
161 }
162
Strftime_l(napi_env env,napi_callback_info info)163 static napi_value Strftime_l(napi_env env, napi_callback_info info)
164 {
165 size_t argc = PARAM_1;
166 napi_value args[1] = {nullptr};
167 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
168 char *valueFirst = NapiHelper::GetString(env, args[0]);
169 static time_t gTime = TIME_L;
170 struct tm *timeptr = localtime(&gTime);
171 char buffer[MAX_NUMBER];
172 locale_t m_locale = newlocale(LC_ALL_MASK, "en_US", nullptr);
173 strftime_l(buffer, sizeof(buffer) - PARAM_1, valueFirst, timeptr, m_locale);
174 napi_value result = nullptr;
175 napi_create_string_utf8(env, buffer, NAPI_AUTO_LENGTH, &result);
176 return result;
177 }
178
Timegm(napi_env env,napi_callback_info info)179 static napi_value Timegm(napi_env env, napi_callback_info info)
180 {
181 time_t timeThis;
182 tzset();
183 static time_t gTime = TIME_L;
184 struct tm *timeptr = localtime(&gTime);
185 timeThis = timegm(timeptr);
186
187 int timeValue = timeThis;
188 napi_value result = nullptr;
189 napi_create_int32(env, timeValue, &result);
190 return result;
191 }
192
TimespecGet(napi_env env,napi_callback_info info)193 static napi_value TimespecGet(napi_env env, napi_callback_info info)
194 {
195 size_t argc = PARAM_1;
196 napi_value args[1] = {nullptr};
197 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
198 int valueFirst;
199 napi_get_value_int32(env, args[0], &valueFirst);
200
201 struct timespec ts;
202 int timeValue = timespec_get(&ts, TIME_UTC);
203 napi_value result = nullptr;
204 napi_create_int32(env, timeValue, &result);
205 return result;
206 }
207
Timer_create(napi_env env,napi_callback_info info)208 static napi_value Timer_create(napi_env env, napi_callback_info info)
209 {
210 size_t argc = PARAM_1;
211 napi_value args[1] = {nullptr};
212 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
213 int valueFirst;
214 napi_get_value_int32(env, args[0], &valueFirst);
215
216 int timeValue = timer_create(FAILNUMBER, nullptr, nullptr);
217 napi_value result = nullptr;
218 napi_create_int32(env, timeValue, &result);
219 return result;
220 }
221
TimerHandler(int sig)222 void TimerHandler(int sig)
223 {
224 if (g_count < MAX_UTIME) {
225 g_count++;
226 }
227 printf("timer handler return\n");
228 return;
229 }
Timer_delete(napi_env env,napi_callback_info info)230 static napi_value Timer_delete(napi_env env, napi_callback_info info)
231 {
232 size_t argc = ONE;
233 napi_value args[1] = {nullptr};
234 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
235 int valueFirst;
236 napi_get_value_int32(env, args[0], &valueFirst);
237
238 timer_t timerid;
239 struct sigevent sev;
240 struct itimerspec its;
241
242 sev.sigev_notify = SIGEV_SIGNAL;
243 sev.sigev_signo = SIGUSR1;
244 sev.sigev_value.sival_ptr = &timerid;
245 signal(SIGUSR1, TimerHandler);
246 timer_create(CLOCK_MONOTONIC, &sev, &timerid);
247
248 its.it_value.tv_sec = ONE;
249 its.it_value.tv_nsec = PARAM_0;
250 its.it_interval.tv_sec = its.it_value.tv_sec;
251 its.it_interval.tv_nsec = its.it_value.tv_nsec;
252 timer_settime(timerid, PARAM_0, &its, nullptr);
253
254 while (g_count != MAX_UTIME) {
255 sleep(PARAM_1);
256 }
257 int timeValue = timer_delete(timerid);
258
259 napi_value result = nullptr;
260 napi_create_int32(env, timeValue, &result);
261 return result;
262 }
263
Timer_gettime(napi_env env,napi_callback_info info)264 static napi_value Timer_gettime(napi_env env, napi_callback_info info)
265 {
266 size_t argc = ONE;
267 napi_value args[1] = {nullptr};
268 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
269 int valueFirst;
270 napi_get_value_int32(env, args[0], &valueFirst);
271
272 int timeValue = timer_gettime(nullptr, nullptr);
273 napi_value result = nullptr;
274 napi_create_int32(env, timeValue, &result);
275 return result;
276 }
277 volatile int t_status = PARAM_0;
handler(int sig)278 void handler(int sig)
279 {
280 g_count++;
281
282 return;
283 }
284
Timer_settime(napi_env env,napi_callback_info info)285 static napi_value Timer_settime(napi_env env, napi_callback_info info)
286 {
287 size_t argc = ONE;
288 napi_value args[1] = {nullptr};
289 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
290 int valueFirst;
291 napi_get_value_int32(env, args[0], &valueFirst);
292 struct sigevent sev;
293 timer_t timerid;
294
295 sev.sigev_notify = SIGEV_SIGNAL;
296 sev.sigev_signo = SIGNUM;
297 sev.sigev_value.sival_ptr = &timerid;
298
299 signal(SIGNUM, handler);
300
301 int result = timer_create(CLOCK_REALTIME, &sev, &timerid);
302 struct itimerspec its;
303 its.it_value.tv_sec = ONE;
304 its.it_value.tv_nsec = PARAM_0;
305 its.it_interval.tv_sec = PARAM_0;
306 its.it_interval.tv_nsec = PARAM_0;
307 result = timer_settime(timerid, PARAM_0, &its, nullptr);
308 while (g_count <= PARAM_0) {
309 sleep(PARAM_1);
310 }
311 result = timer_delete(timerid);
312
313 napi_value results = nullptr;
314 napi_create_int32(env, result, &results);
315 return results;
316 }
Timer_getoverrun(napi_env env,napi_callback_info info)317 static napi_value Timer_getoverrun(napi_env env, napi_callback_info info)
318 {
319 size_t argc = ONE;
320 napi_value args[1] = {nullptr};
321 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
322 int valueFirst;
323 napi_get_value_int32(env, args[0], &valueFirst);
324
325 int timeValue = timer_getoverrun(nullptr);
326 napi_value result = nullptr;
327 napi_create_int32(env, timeValue, &result);
328 return result;
329 }
330
Strptime(napi_env env,napi_callback_info info)331 static napi_value Strptime(napi_env env, napi_callback_info info)
332 {
333 size_t argc = ONE;
334 napi_value args[1] = {nullptr};
335 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
336 char *valueFirst = NapiHelper::GetString(env, args[0]);
337 const char *buffer = "1";
338 struct tm tmTime = {0};
339 char *cnt = strptime(buffer, valueFirst, &tmTime);
340 napi_value result = nullptr;
341 napi_create_string_utf8(env, cnt, NAPI_AUTO_LENGTH, &result);
342 return result;
343 }
344
Time(napi_env env,napi_callback_info info)345 static napi_value Time(napi_env env, napi_callback_info info)
346 {
347 time_t timeValue = time(nullptr);
348
349 napi_value result = nullptr;
350
351 napi_create_double(env, timeValue, &result);
352
353 return result;
354 }
355
GetdateErr(napi_env env,napi_callback_info info)356 static napi_value GetdateErr(napi_env env, napi_callback_info info)
357 {
358 getdate_err = PARAM_1;
359 int resultValue = abs(getdate_err);
360 napi_value result = nullptr;
361 napi_create_int32(env, resultValue, &result);
362
363 return result;
364 }
365
Daylight(napi_env env,napi_callback_info info)366 static napi_value Daylight(napi_env env, napi_callback_info info)
367 {
368 daylight = PARAM_1;
369 int resultValue = abs(daylight);
370 napi_value result = nullptr;
371 napi_create_int32(env, resultValue, &result);
372
373 return result;
374 }
375
Tzset(napi_env env,napi_callback_info info)376 static napi_value Tzset(napi_env env, napi_callback_info info)
377 {
378 int toJs = FAIL;
379 time_t td;
380 time_t td1;
381 time_t td2;
382 char pch[] = "TZ=US/Pacific";
383 char lg[] = "TZ=America/Los_Angeles";
384 char pst[] = "TZ=PST8PDT";
385 putenv(pch);
386 tzset();
387 time(&td);
388 std::string usTimeString = asctime(localtime(&td));
389
390 putenv(lg);
391 tzset();
392 time(&td1);
393 std::string amerTimeString = asctime(localtime(&td1));
394
395 putenv(pst);
396 tzset();
397 time(&td2);
398 std::string pstTimeString = asctime(localtime(&td2));
399
400 if (usTimeString != amerTimeString || usTimeString != pstTimeString || amerTimeString != pstTimeString) {
401 toJs = SUCCESS;
402 }
403 napi_value result = nullptr;
404 napi_create_int32(env, toJs, &result);
405 return result;
406 }
407
DiffTime(napi_env env,napi_callback_info info)408 static napi_value DiffTime(napi_env env, napi_callback_info info)
409 {
410 time_t now;
411 struct tm newyear;
412 double seconds;
413 time(&now);
414 newyear = *localtime(&now);
415 newyear.tm_hour = PARAM_0;
416 newyear.tm_min = PARAM_0;
417 newyear.tm_sec = PARAM_0;
418 newyear.tm_mon = PARAM_0;
419 newyear.tm_mday = PARAM_1;
420 time_t firstTime = PARAM_1999 - PARAM_12 - PARAM_17 - PARAM_06 - PARAM_30 - PARAM_23;
421 time_t secondTime = PARAM_1900 - PARAM_02 - PARAM_07 - PARAM_16 - PARAM_15 - PARAM_20;
422 seconds = difftime(firstTime, secondTime);
423 napi_value result = nullptr;
424 napi_create_double(env, seconds, &result);
425 return result;
426 }
427
CTime_r(napi_env env,napi_callback_info info)428 static napi_value CTime_r(napi_env env, napi_callback_info info)
429 {
430 char *checkParam = nullptr;
431 int returnParam = PARAM_1;
432 const time_t time = PARAM_0;
433 char secondParam[32];
434 checkParam = ctime_r(&time, secondParam);
435 if (checkParam != nullptr) {
436 returnParam = PARAM_0;
437 }
438 napi_value result = nullptr;
439 napi_create_int32(env, returnParam, &result);
440 return result;
441 }
442
CTime(napi_env env,napi_callback_info info)443 static napi_value CTime(napi_env env, napi_callback_info info)
444 {
445 char *checkParam = nullptr;
446 int returnParam = PARAM_1;
447 const time_t time = PARAM_0;
448 checkParam = ctime(&time);
449 if (checkParam != nullptr) {
450 returnParam = PARAM_0;
451 }
452 napi_value result = nullptr;
453 napi_create_int32(env, returnParam, &result);
454 return result;
455 }
456
Asctime(napi_env env,napi_callback_info info)457 napi_value Asctime(napi_env env, napi_callback_info info)
458 {
459 time_t curr_time;
460 time(&curr_time);
461 char *time_str = asctime(localtime(&curr_time));
462 int resultValue = FAIL;
463 if (time_str != nullptr) {
464 resultValue = SUCCESS;
465 }
466 napi_value result = nullptr;
467 napi_create_int32(env, resultValue, &result);
468 return result;
469 }
470
AsctimeR(napi_env env,napi_callback_info info)471 napi_value AsctimeR(napi_env env, napi_callback_info info)
472 {
473 time_t rawtime;
474 struct tm *timeinfo = nullptr;
475 char buffer[80];
476
477 time(&rawtime);
478 timeinfo = localtime(&rawtime);
479 if (timeinfo == nullptr) {
480 return nullptr;
481 }
482 char *resultVal = asctime_r(timeinfo, buffer);
483 int resultValue = FAIL;
484 if (resultVal != nullptr) {
485 resultValue = SUCCESS;
486 }
487 napi_value result = nullptr;
488 napi_create_int32(env, resultValue, &result);
489 return result;
490 }
491
Clock(napi_env env,napi_callback_info info)492 static napi_value Clock(napi_env env, napi_callback_info info)
493 {
494 clock_t returnValue = clock();
495 int param = FAIL;
496 if (returnValue >= PARAM_0) {
497 param = SUCCESS;
498 }
499 napi_value result;
500 napi_create_int32(env, param, &result);
501 return result;
502 }
503
ClockAdjtime(napi_env env,napi_callback_info info)504 static napi_value ClockAdjtime(napi_env env, napi_callback_info info)
505 {
506 errno = ERRON_0;
507 struct timex tx;
508 memset(&tx, PARAM_0, sizeof(tx));
509
510 int value = clock_adjtime(CLOCK_REALTIME, &tx);
511 int returnValue = FAIL;
512 if (value != FAIL) {
513 returnValue = SUCCESS;
514 }
515 napi_value result;
516 napi_create_int32(env, returnValue, &result);
517 return result;
518 }
519
ClockGetcpuclockid(napi_env env,napi_callback_info info)520 static napi_value ClockGetcpuclockid(napi_env env, napi_callback_info info)
521 {
522 errno = ERRON_0;
523 clockid_t clk;
524 pid_t pid = getppid();
525
526 clock_getcpuclockid(pid, &clk);
527 int returnValue = FAIL;
528 if (errno == ERRON_0) {
529 returnValue = SUCCESS;
530 }
531 napi_value result;
532 napi_create_int32(env, returnValue, &result);
533 return result;
534 }
535
ClockGetres(napi_env env,napi_callback_info info)536 static napi_value ClockGetres(napi_env env, napi_callback_info info)
537 {
538 errno = ERRON_0;
539 struct timespec ts;
540 memset_s(&ts, sizeof(ts), 0, sizeof(ts));
541 int value = clock_getres(CLOCK_REALTIME, &ts);
542 napi_value result;
543 napi_create_int32(env, value, &result);
544 return result;
545 }
546
ClockGettime(napi_env env,napi_callback_info info)547 static napi_value ClockGettime(napi_env env, napi_callback_info info)
548 {
549 struct timespec ts;
550 memset_s(&ts, sizeof(ts), 0, sizeof(ts));
551 int value = clock_gettime(CLOCK_REALTIME, &ts);
552 napi_value result;
553 napi_create_int32(env, value, &result);
554 return result;
555 }
556
ClockNanosleep(napi_env env,napi_callback_info info)557 static napi_value ClockNanosleep(napi_env env, napi_callback_info info)
558 {
559 errno = ERRON_0;
560 struct timespec ts;
561 memset_s(&ts, sizeof(ts), 0, sizeof(ts));
562 ts.tv_sec = ts.tv_nsec / TVSEC;
563 ts.tv_nsec = TVNSEC;
564 int ret = FAIL;
565 ret = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &ts, nullptr);
566 napi_value result;
567 napi_create_int32(env, ret, &result);
568 return result;
569 }
570
ClockSettime(napi_env env,napi_callback_info info)571 static napi_value ClockSettime(napi_env env, napi_callback_info info)
572 {
573 napi_value result;
574 napi_create_int32(env, NO_ERR, &result);
575 return result;
576 }
577
Localtime(napi_env env,napi_callback_info info)578 static napi_value Localtime(napi_env env, napi_callback_info info)
579 {
580 struct tm *ptm = nullptr;
581 time_t ttv = PARAM_0;
582
583 ptm = localtime(&ttv);
584
585 napi_value result;
586 if (ptm == nullptr) {
587 napi_create_int32(env, FAIL, &result);
588 } else {
589 napi_create_int32(env, NO_ERR, &result);
590 }
591 return result;
592 }
593
LocaltimeR(napi_env env,napi_callback_info info)594 static napi_value LocaltimeR(napi_env env, napi_callback_info info)
595 {
596 struct tm *ptm = (struct tm *)malloc(sizeof(struct tm));
597 time_t ttv = PARAM_0;
598
599 ptm = localtime_r(&ttv, ptm);
600
601 napi_value result;
602 if (ptm == nullptr) {
603 napi_create_int32(env, FAIL, &result);
604 } else {
605 napi_create_int32(env, NO_ERR, &result);
606 }
607 return result;
608 }
609
MkTime(napi_env env,napi_callback_info info)610 static napi_value MkTime(napi_env env, napi_callback_info info)
611 {
612 struct tm *ptm = (struct tm *)malloc(sizeof(struct tm));
613 time_t ttv = PARAM_0;
614
615 ttv = mktime(ptm);
616
617 napi_value result;
618 if (ttv == FAILD) {
619 napi_create_int32(env, FAIL, &result);
620 } else {
621 napi_create_int32(env, NO_ERR, &result);
622 }
623 return result;
624 }
625
Gmtime(napi_env env,napi_callback_info info)626 static napi_value Gmtime(napi_env env, napi_callback_info info)
627 {
628 time_t rawtime;
629 struct tm *infoat;
630
631 time(&rawtime);
632 /* 获取 GMT 时间 */
633 infoat = gmtime(&rawtime);
634 napi_value result;
635 if (infoat == nullptr) {
636 napi_create_int32(env, FAIL, &result);
637 } else {
638 napi_create_int32(env, NO_ERR, &result);
639 }
640 return result;
641 }
642
GmtimeR(napi_env env,napi_callback_info info)643 static napi_value GmtimeR(napi_env env, napi_callback_info info)
644 {
645 time_t time = std::time(nullptr);
646 struct tm parsed_time = {0};
647 struct tm gmt_parsed_time = {0};
648 napi_value result;
649
650 if (localtime_r(&time, &parsed_time) == nullptr) {
651 napi_create_int32(env, FAIL, &result);
652 return result;
653 }
654
655 if (gmtime_r(&time, &gmt_parsed_time) == nullptr) {
656 napi_create_int32(env, FAIL, &result);
657 return result;
658 }
659
660 napi_create_int32(env, NO_ERR, &result);
661 return result;
662 }
663
Lutimes(napi_env env,napi_callback_info info)664 static napi_value Lutimes(napi_env env, napi_callback_info info)
665 {
666 int ret;
667 struct stat s;
668 static struct timeval tv[TEST_SIZE] = {{0L, 0L}, {0L, 0L}};
669 tv[0].tv_sec = s.st_atime;
670 tv[0].tv_usec = PARAM_0;
671 tv[1].tv_sec = s.st_mtime;
672 tv[1].tv_usec = PARAM_0;
673 int fd = open("/data/storage/el2/base/files/Fzl.txt", O_RDWR | O_CREAT, SIZE_0666);
674 close(fd);
675 ret = lutimes("/data/storage/el2/base/files/Fzl.txt", tv);
676 napi_value result;
677 if (ret == FAILD) {
678 napi_create_int32(env, FAIL, &result);
679 } else {
680 napi_create_int32(env, NO_ERR, &result);
681 }
682 return result;
683 }
684
Nanosleep(napi_env env,napi_callback_info info)685 static napi_value Nanosleep(napi_env env, napi_callback_info info)
686 {
687 struct timespec n_sleep;
688 struct timespec p_sleep;
689 n_sleep.tv_sec = PARAM_0;
690 n_sleep.tv_nsec = 5e8L;
691 int ret = nanosleep(&n_sleep, &p_sleep);
692 napi_value result;
693 napi_create_int32(env, ret, &result);
694 return result;
695 }
696
TimeZone(napi_env env,napi_callback_info info)697 static napi_value TimeZone(napi_env env, napi_callback_info info)
698 {
699 errno = NO_ERR;
700 struct timeval start;
701 struct timezone tz;
702 int gitInfo = gettimeofday(&start, &tz);
703
704 napi_value result = nullptr;
705 napi_create_int32(env, gitInfo, &result);
706 return result;
707 }
708
Settimeofday(napi_env env,napi_callback_info info)709 static napi_value Settimeofday(napi_env env, napi_callback_info info)
710 {
711 int setval = settimeofday(nullptr, nullptr);
712 napi_value result;
713 napi_create_int32(env, setval, &result);
714 return result;
715 }
716 EXTERN_C_START
Init(napi_env env,napi_value exports)717 static napi_value Init(napi_env env, napi_value exports)
718 {
719 napi_property_descriptor desc[] = {
720 {"getITimer", nullptr, GetITimer, nullptr, nullptr, nullptr, napi_default, nullptr},
721 {"getTimeOfDay", nullptr, GetTimeOfDay, nullptr, nullptr, nullptr, napi_default, nullptr},
722 {"setitimer", nullptr, Setitimer, nullptr, nullptr, nullptr, napi_default, nullptr},
723 {"localtime", nullptr, Localtime, nullptr, nullptr, nullptr, napi_default, nullptr},
724 {"localtimeR", nullptr, LocaltimeR, nullptr, nullptr, nullptr, napi_default, nullptr},
725 {"mkTime", nullptr, MkTime, nullptr, nullptr, nullptr, napi_default, nullptr},
726 {"gmtime", nullptr, Gmtime, nullptr, nullptr, nullptr, napi_default, nullptr},
727 {"gmtimeR", nullptr, GmtimeR, nullptr, nullptr, nullptr, napi_default, nullptr},
728 {"lutimes", nullptr, Lutimes, nullptr, nullptr, nullptr, napi_default, nullptr},
729 {"nanosleep", nullptr, Nanosleep, nullptr, nullptr, nullptr, napi_default, nullptr},
730 {"timeZone", nullptr, TimeZone, nullptr, nullptr, nullptr, napi_default, nullptr},
731 {"strftime", nullptr, Strftime, nullptr, nullptr, nullptr, napi_default, nullptr},
732 {"strftime_l", nullptr, Strftime_l, nullptr, nullptr, nullptr, napi_default, nullptr},
733 {"timegm", nullptr, Timegm, nullptr, nullptr, nullptr, napi_default, nullptr},
734 {"timespec_get", nullptr, TimespecGet, nullptr, nullptr, nullptr, napi_default, nullptr},
735 {"timer_create", nullptr, Timer_create, nullptr, nullptr, nullptr, napi_default, nullptr},
736 {"timer_delete", nullptr, Timer_delete, nullptr, nullptr, nullptr, napi_default, nullptr},
737 {"timer_gettime", nullptr, Timer_gettime, nullptr, nullptr, nullptr, napi_default, nullptr},
738 {"timer_settime", nullptr, Timer_settime, nullptr, nullptr, nullptr, napi_default, nullptr},
739 {"timer_getoverrun", nullptr, Timer_getoverrun, nullptr, nullptr, nullptr, napi_default, nullptr},
740 {"strptime", nullptr, Strptime, nullptr, nullptr, nullptr, napi_default, nullptr},
741 {"tzset", nullptr, Tzset, nullptr, nullptr, nullptr, napi_default, nullptr},
742 {"difftime", nullptr, DiffTime, nullptr, nullptr, nullptr, napi_default, nullptr},
743 {"ctime", nullptr, CTime, nullptr, nullptr, nullptr, napi_default, nullptr},
744 {"ctime_r", nullptr, CTime_r, nullptr, nullptr, nullptr, napi_default, nullptr},
745 {"asctime", nullptr, Asctime, nullptr, nullptr, nullptr, napi_default, nullptr},
746 {"asctimeR", nullptr, AsctimeR, nullptr, nullptr, nullptr, napi_default, nullptr},
747 {"clock", nullptr, Clock, nullptr, nullptr, nullptr, napi_default, nullptr},
748 {"clockAdjtime", nullptr, ClockAdjtime, nullptr, nullptr, nullptr, napi_default, nullptr},
749 {"clockGetcpuclockid", nullptr, ClockGetcpuclockid, nullptr, nullptr, nullptr, napi_default, nullptr},
750 {"clockGetres", nullptr, ClockGetres, nullptr, nullptr, nullptr, napi_default, nullptr},
751 {"clockGettime", nullptr, ClockGettime, nullptr, nullptr, nullptr, napi_default, nullptr},
752 {"clockNanosleep", nullptr, ClockNanosleep, nullptr, nullptr, nullptr, napi_default, nullptr},
753 {"clockSettime", nullptr, ClockSettime, nullptr, nullptr, nullptr, napi_default, nullptr},
754 {"settimeofday", nullptr, Settimeofday, nullptr, nullptr, nullptr, napi_default, nullptr},
755 {"time", nullptr, Time, nullptr, nullptr, nullptr, napi_default, nullptr},
756 {"getdateErr", nullptr, GetdateErr, nullptr, nullptr, nullptr, napi_default, nullptr},
757 {"daylight", nullptr, Daylight, nullptr, nullptr, nullptr, napi_default, nullptr},
758 };
759 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
760 return exports;
761 }
762 EXTERN_C_END
763
764 static napi_module demoModule = {
765 .nm_version = 1,
766 .nm_flags = 0,
767 .nm_filename = nullptr,
768 .nm_register_func = Init,
769 .nm_modname = "time",
770 .nm_priv = ((void *)0),
771 .reserved = {0},
772 };
773
RegisterModule(void)774 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
775