summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorChris Wilson <chris+github@qwirx.com>2017-05-11 20:50:26 +0100
committerChris Wilson <chris+github@qwirx.com>2017-06-22 20:28:06 +0100
commit809d9ebacc409c4a86edb6077f489da67a2653d4 (patch)
tree0d2c3bb4422a5362f2617f916a94c8430b0b4996 /lib
parent105f2620b98bfd4e5d7ed576b5cc4b2317dcf634 (diff)
Increase precision of timing on Windows.
Should hopefully reduce random failures of timing-dependent tests which rely on subsecond precision for accuracy. (cherry picked from commit c845b2e39fffeb560983a301d810616a6495469d)
Diffstat (limited to 'lib')
-rw-r--r--lib/common/BoxTime.cpp39
1 files changed, 24 insertions, 15 deletions
diff --git a/lib/common/BoxTime.cpp b/lib/common/BoxTime.cpp
index 78269def..77daae6d 100644
--- a/lib/common/BoxTime.cpp
+++ b/lib/common/BoxTime.cpp
@@ -35,21 +35,30 @@
// --------------------------------------------------------------------------
box_time_t GetCurrentBoxTime()
{
- #ifdef HAVE_GETTIMEOFDAY
- struct timeval tv;
- if (gettimeofday(&tv, NULL) != 0)
- {
- BOX_LOG_SYS_ERROR("Failed to gettimeofday(), "
- "dropping precision");
- }
- else
- {
- box_time_t timeNow = (tv.tv_sec * MICRO_SEC_IN_SEC_LL)
- + tv.tv_usec;
- return timeNow;
- }
- #endif
-
+#ifdef HAVE_GETTIMEOFDAY
+ struct timeval tv;
+ if (gettimeofday(&tv, NULL) != 0)
+ {
+ BOX_LOG_SYS_ERROR("Failed to gettimeofday(), "
+ "dropping precision");
+ }
+ else
+ {
+ box_time_t time_now = (tv.tv_sec * MICRO_SEC_IN_SEC_LL) + tv.tv_usec;
+ return time_now;
+ }
+#elif WIN32
+ // There's no Win32 API function that returns the current time as a UNIX timestamp with
+ // sub-second precision. So we use time(0) and add the fractional part from
+ // GetSystemTime() in the hope that the difference between these two (if any) is a whole
+ // number of seconds.
+ box_time_t time_now = SecondsToBoxTime(time(0));
+ SYSTEMTIME system_time;
+ GetSystemTime(&system_time);
+ time_now += MilliSecondsToBoxTime(system_time.wMilliseconds);
+ return time_now;
+#endif
+
return SecondsToBoxTime(time(0));
}