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