on master help sign in
Implement idris_clock on windows
7626280Niklas Larsson
libs/base/System.idr−1
@@ -100,7 +100,6 @@ record Clock where
100 nanoseconds : Integer
100 nanoseconds : Integer
101
101
102||| Get the system's wall clock time.
102||| Get the system's wall clock time.
103−||| At the moment, this returns 0 on Windows
104clockTime : IO Clock
103clockTime : IO Clock
105clockTime
104clockTime
106 = do vm <- getMyVM
105 = do vm <- getMyVM
rts/idris_stdfgn.c+5−5
@@ -11,9 +11,7 @@
11#include <unistd.h>
11#include <unistd.h>
12
12
13#ifdef _WIN32
13#ifdef _WIN32
14−int win_fpoll(void* h);
14+#include "windows/win_utils.h"
15−FILE *win32_u8fopen(const char *path, const char *mode);
16−FILE *win32_u8popen(const char *path, const char *mode);
17#else
15#else
18#include <sys/select.h>
16#include <sys/select.h>
19#endif
17#endif
@@ -209,9 +207,11 @@ VAL idris_time() {
209VAL idris_clock(VM* vm) {
207VAL idris_clock(VM* vm) {
210 VAL result;
208 VAL result;
211#ifdef _WIN32
209#ifdef _WIN32
210+ int64_t sec, nsec;
211+ win32_gettime(&sec, &nsec);
212 idris_constructor(result, vm, 0, 2, 0);
212 idris_constructor(result, vm, 0, 2, 0);
213− idris_setConArg(result, 0, MKBIGI(0));
213+ idris_setConArg(result, 0, MKBIGI(sec));
214− idris_setConArg(result, 1, MKBIGI(0));
214+ idris_setConArg(result, 1, MKBIGI(nsec));
215#else
215#else
216 struct timespec ts;
216 struct timespec ts;
217 // We're not checking the result here, which is of course bad, but
217 // We're not checking the result here, which is of course bad, but
rts/windows/win_utils.c+16
@@ -1,7 +1,9 @@
1#include <io.h>
1#include <io.h>
2+#include <stdint.h>
2#include <stdio.h>
3#include <stdio.h>
3#include <windows.h>
4#include <windows.h>
4
5
6+
5// THis file exists to avoid clashes between windows.h and idris_rts.h
7// THis file exists to avoid clashes between windows.h and idris_rts.h
6//
8//
7
9
@@ -28,6 +30,7 @@ int widen_utf8(const char *filename_utf8, LPWSTR *filename_w)
28 MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, *filename_w, num_chars);
30 MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, *filename_w, num_chars);
29 return num_chars;
31 return num_chars;
30}
32}
33+
31FILE *win32_u8fopen(const char *path, const char *mode)
34FILE *win32_u8fopen(const char *path, const char *mode)
32{
35{
33 LPWSTR wpath, wmode;
36 LPWSTR wpath, wmode;
@@ -49,3 +52,16 @@ FILE *win32_u8popen(const char *path, const char *mode)
49 free(wmode);
52 free(wmode);
50 return f;
53 return f;
51}
54}
55+
56+void win32_gettime(int64_t* sec, int64_t* nsec)
57+{
58+ FILETIME ft;
59+ GetSystemTimePreciseAsFileTime(&ft);
60+ ULARGE_INTEGER t;
61+ t.HighPart = ft.dwHighDateTime;
62+ t.LowPart = ft.dwLowDateTime;
63+
64+ *nsec = (t.QuadPart % 10000000)*100;
65+ *sec = t.QuadPart / 10000000;
66+ *sec -= 11644473600; // LDAP epoch to Unix epoch
67+}
rts/windows/win_utils.hnew file+9
@@ -0,0 +1,9 @@
1+#include <stdint.h>
2+#include <stdio.h>
3+
4+#pragma once
5+
6+int win_fpoll(void *h);
7+FILE *win32_u8fopen(const char *path, const char *mode);
8+FILE *win32_u8popen(const char *path, const char *mode);
9+void win32_gettime(int64_t* sec, int64_t* nsec);