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