on master help sign in
Add Clock/clockTime in System
62bd431Edwin Brady
libs/base/System.idr+14
@@ -93,6 +93,20 @@ time : IO Integer
93time = do MkRaw t <- foreign FFI_C "idris_time" (IO (Raw Integer))
93time = do MkRaw t <- foreign FFI_C "idris_time" (IO (Raw Integer))
94 pure t
94 pure t
95
95
96+||| Wall clock time
97+record Clock where
98+ constructor MkClock
99+ seconds : Integer
100+ nanoseconds : Integer
101+
102+||| Get the system's wall clock time.
103+||| At the moment, this returns 0 on Windows
104+clockTime : IO Clock
105+clockTime
106+ = do vm <- getMyVM
107+ MkRaw c <- foreign FFI_C "idris_clock" (Ptr -> IO (Raw Clock)) vm
108+ pure c
109+
96||| Specify interval to sleep for in microseconds, must be nonnegative
110||| Specify interval to sleep for in microseconds, must be nonnegative
97usleep : (i : Int) -> { auto prf : So (i >= 0) } -> IO ()
111usleep : (i : Int) -> { auto prf : So (i >= 0) } -> IO ()
98usleep i = foreign FFI_C "idris_usleep" (Int -> IO ()) i
112usleep i = foreign FFI_C "idris_usleep" (Int -> IO ()) i
rts/idris_stdfgn.c+18
@@ -206,6 +206,24 @@ VAL idris_time() {
206 return MKBIGI(t);
206 return MKBIGI(t);
207}
207}
208
208
209+VAL idris_clock(VM* vm) {
210+ VAL result;
211+#ifdef _WIN32
212+ idris_constructor(result, vm, 0, 2, 0);
213+ idris_setConArg(result, 0, MKBIGI(0));
214+ idris_setConArg(result, 1, MKBIGI(0));
215+#else
216+ struct timespec ts;
217+ // We're not checking the result here, which is of course bad, but
218+ // CLOCK_REALTIME is required by POSIX at least!
219+ clock_gettime(CLOCK_REALTIME, &ts);
220+ idris_constructor(result, vm, 0, 2, 0);
221+ idris_setConArg(result, 0, MKBIGI(ts.tv_sec));
222+ idris_setConArg(result, 1, MKBIGI(ts.tv_nsec));
223+#endif
224+ return result;
225+}
226+
209VAL idris_mkFileError(VM* vm) {
227VAL idris_mkFileError(VM* vm) {
210 VAL result;
228 VAL result;
211 switch(errno) {
229 switch(errno) {
rts/idris_stdfgn.h+1
@@ -52,6 +52,7 @@ void* idris_stdin();
52char* getEnvPair(int i);
52char* getEnvPair(int i);
53
53
54VAL idris_time();
54VAL idris_time();
55+VAL idris_clock(VM* vm);
55
56
56void idris_forceGC();
57void idris_forceGC();
57
58