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