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