on master help sign in
Merge pull request #4701 from melted/time_win

Implement idris_clock on windows

commitc8f7604864c0f98ab09187dd061ff3ffbaec9df1 authorNiklas Larsson <niklas@mm.st> dateThu 9 May 2019, 08:11 (7 years ago) parent5eebc7b7626280
4 files changed+30−6
libs/base/System.idr−1 rts/idris_stdfgn.c+5−5 rts/windows/win_utils.c+16 rts/windows/win_utils.h+9
libs/base/System.idr−1
@@ -100,7 +100,6 @@ record Clock where
100100 nanoseconds : Integer
101101
102102||| Get the system's wall clock time.
103−||| At the moment, this returns 0 on Windows
104103clockTime : IO Clock
105104clockTime
106105 = do vm <- getMyVM
rts/idris_stdfgn.c+5−5
@@ -11,9 +11,7 @@
1111#include <unistd.h>
1212
1313#ifdef _WIN32
14−int win_fpoll(void* h);
15−FILE *win32_u8fopen(const char *path, const char *mode);
16−FILE *win32_u8popen(const char *path, const char *mode);
14+#include "windows/win_utils.h"
1715#else
1816#include <sys/select.h>
1917#endif
@@ -209,9 +207,11 @@ VAL idris_time() {
209207VAL idris_clock(VM* vm) {
210208 VAL result;
211209#ifdef _WIN32
210+ int64_t sec, nsec;
211+ win32_gettime(&sec, &nsec);
212212 idris_constructor(result, vm, 0, 2, 0);
213− idris_setConArg(result, 0, MKBIGI(0));
214− idris_setConArg(result, 1, MKBIGI(0));
213+ idris_setConArg(result, 0, MKBIGI(sec));
214+ idris_setConArg(result, 1, MKBIGI(nsec));
215215#else
216216 struct timespec ts;
217217 // We're not checking the result here, which is of course bad, but
rts/windows/win_utils.c+16
@@ -1,7 +1,9 @@
11#include <io.h>
2+#include <stdint.h>
23#include <stdio.h>
34#include <windows.h>
45
6+
57// THis file exists to avoid clashes between windows.h and idris_rts.h
68//
79
@@ -28,6 +30,7 @@ int widen_utf8(const char *filename_utf8, LPWSTR *filename_w)
2830 MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, *filename_w, num_chars);
2931 return num_chars;
3032}
33+
3134FILE *win32_u8fopen(const char *path, const char *mode)
3235{
3336 LPWSTR wpath, wmode;
@@ -49,3 +52,16 @@ FILE *win32_u8popen(const char *path, const char *mode)
4952 free(wmode);
5053 return f;
5154}
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);