| View Issue Details [ Jump to Notes ] | [ Issue History ] [ Print ] |
| ID | Project | Category | View Status | Date Submitted | Last Update |
| 0005734 | OCaml | OCaml windows | public | 2012-08-19 18:07 | 2012-09-24 13:39 |
|
| Reporter | dra | |
| Assigned To | | |
| Priority | normal | Severity | tweak | Reproducibility | always |
| Status | resolved | Resolution | fixed | |
| Platform | | OS | | OS Version | |
| Product Version | 3.12.1 | |
| Target Version | 4.01.0+dev | Fixed in Version | 4.01.0+dev | |
|
| Summary | 0005734: Unix.gettimeofday correct resolution under Windows |
| Description | Current implementation of Unix.gettimeofday is not accurate as it is initialised with a time source with a resolution in seconds. |
| Steps To Reproduce | First call to Unix.gettimeofday under Windows always returns a time rounded to the nearest number of seconds. |
| Additional Information | The attached patch uses the Win32 API GetLocalTime (*not* GetSystemTime as the result is used to pass a struct to mktime) which returns the current time to the nearest millisecond. This is used to initialise initial_time in gettimeofday.c
Patch was made against 3.12.1, but the file is unchanged in trunk. Patch respects m.h's definition of HAS_MKTIME but this is redundant under Windows as it always does have MKTIME.
Note that using GetTickCount means that Unix.gettimeofday never responds to the local clock being corrected *forwards*. For long lived processes this could be a problem? GetTickCount is recommended for measuring elapsed time - shouldn't Unix.gettimeofday always query the local clock? |
| Tags | No tags attached. |
|
| Attached Files | ocaml-3.12.1-GetTimeOfDay.patch [^] (1,272 bytes) 2012-08-19 18:07 [Show Content] [Hide Content]diff -Naur ocaml-3.12.1.old/otherlibs/win32unix/gettimeofday.c ocaml-3.12.1/otherlibs/win32unix/gettimeofday.c
--- ocaml-3.12.1.old/otherlibs/win32unix/gettimeofday.c 2010-01-22 12:48:24.000000000 +0000
+++ ocaml-3.12.1/otherlibs/win32unix/gettimeofday.c 2012-08-19 16:42:29.529465200 +0100
@@ -19,15 +19,35 @@
#include "unixsupport.h"
+#ifdef HAS_MKTIME
+static double initial_time = 0; /* 0 means uninitialized */
+#else
static time_t initial_time = 0; /* 0 means uninitialized */
+#endif
static DWORD initial_tickcount;
CAMLprim value unix_gettimeofday(value unit)
{
DWORD tickcount = GetTickCount();
+ SYSTEMTIME st;
+ struct tm tm;
if (initial_time == 0 || tickcount < initial_tickcount) {
initial_tickcount = tickcount;
+#ifdef HAS_MKTIME
+ GetLocalTime(&st);
+ tm.tm_sec = st.wSecond;
+ tm.tm_min = st.wMinute;
+ tm.tm_hour = st.wHour;
+ tm.tm_mday = st.wDay;
+ tm.tm_mon = st.wMonth - 1;
+ tm.tm_year = st.wYear - 1900;
+ tm.tm_wday = 0;
+ tm.tm_yday = 0;
+ tm.tm_isdst = -1;
+ initial_time = ((double) mktime(&tm) + (double) st.wMilliseconds * 1e-3);
+#else
initial_time = time(NULL);
+#endif
return copy_double((double) initial_time);
} else {
return copy_double((double) initial_time +
|
|