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