ifdef HAVE_INT128
[m6w6/ext-psi] / psi.d / time.psi
1 #include <sys/time.h>
2 #include <time.h>
3 #include <sys/times.h>
4
5 // extern time_t time(time_t *t);
6 function psi\time() : int {
7 let t = NULL;
8 return time(t) as to_int(time);
9 }
10
11 // extern int gettimeofday(struct timeval *tp, struct timezone *tz);
12 function psi\gettimeofday(array &$tv = NULL, array &$tz = NULL) : int {
13 let tp = calloc(1, sizeof(struct timeval));
14 let tz = calloc(1, sizeof(struct timezone));
15 return gettimeofday(tp, tz) as to_int(gettimeofday);
16 set $tv = to_array(*tp,
17 to_int(tv_sec),
18 to_int(tv_usec));
19 set $tz = to_array(*tz,
20 to_int(tz_minuteswest),
21 to_int(tz_dsttime));
22 }
23
24 // extern char *asctime(struct tm *tm);
25 function psi\asctime(array $tm) : string {
26 let tm = &arrval($tm,
27 intval($tm_sec),
28 intval($tm_min),
29 intval($tm_hour),
30 intval($tm_mday),
31 intval($tm_mon),
32 intval($tm_year),
33 intval($tm_wday),
34 intval($tm_yday),
35 intval($tm_isdst)
36 );
37 return asctime(tm) as to_string(asctime);
38 }
39
40 // extern char *asctime_r(struct tm *tm, char *buf);
41 function psi\asctime_r(array $tm) : string {
42 let tm = &arrval($tm,
43 intval($tm_sec),
44 intval($tm_min),
45 intval($tm_hour),
46 intval($tm_mday),
47 intval($tm_mon),
48 intval($tm_year),
49 intval($tm_wday),
50 intval($tm_yday),
51 intval($tm_isdst)
52 );
53 let buf = calloc(32, sizeof(char));
54 return asctime_r(tm, buf) as to_string(asctime_r);
55 }
56
57 // extern struct tm *gmtime(time_t *t);
58 function psi\gmtime(int $ts) : array {
59 let t = &intval($ts);
60 return gmtime(t) as to_array(*gmtime,
61 to_int(tm_sec),
62 to_int(tm_min),
63 to_int(tm_hour),
64 to_int(tm_mday),
65 to_int(tm_mon),
66 to_int(tm_year),
67 to_int(tm_wday),
68 to_int(tm_yday),
69 to_int(tm_isdst)
70 );
71 }
72
73 // extern struct tm *gmtime_r(time_t *t, struct tm *buf);
74 function psi\gmtime_r(int $ts) : array {
75 let t = &intval($ts);
76 let buf = calloc(1, sizeof(struct tm));
77 return gmtime_r(t, buf) as to_array(*gmtime_r,
78 to_int(tm_sec),
79 to_int(tm_min),
80 to_int(tm_hour),
81 to_int(tm_mday),
82 to_int(tm_mon),
83 to_int(tm_year),
84 to_int(tm_wday),
85 to_int(tm_yday),
86 to_int(tm_isdst)
87 );
88 }
89
90 // extern int nanosleep(struct timespec *rqts, struct timespec *rmts);
91 function psi\nanosleep(array $rq = NULL, array &$rm = NULL) : int {
92 let rqts = &arrval($rq,
93 intval($tv_sec),
94 intval($tv_nsec)
95 );
96 let rmts = calloc(1, sizeof(struct timespec));
97 return nanosleep(rqts, rmts) as to_int(nanosleep);
98 set $rm = to_array(*rmts,
99 to_int(tv_sec),
100 to_int(tv_nsec)
101 );
102 }
103
104
105 // extern clock_t times(struct tms *buf);
106 function psi\times(array &$tms = NULL) : int {
107 let buf = calloc(1, sizeof(struct tms));
108 return times(buf) as to_int(times);
109 set $tms = to_array(*buf,
110 to_int(tms_utime),
111 to_int(tms_stime),
112 to_int(tms_cutime),
113 to_int(tms_cstime)
114 );
115 }