From 940a96716cddec62ffbeff31decd3c662604a9d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=B6fferlein?= Date: Sun, 9 Sep 2018 18:30:21 +0200 Subject: [PATCH] Fixed the timestamp generation for pthread-win32 timed waits Issue: we have to use WIN API functions which deliver times in a different way. In order to make the wait functions work, we have to use the same time definition - specifically the same zero time. --- src/tl/tl/tlTimer.cc | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/tl/tl/tlTimer.cc b/src/tl/tl/tlTimer.cc index 6c612ee07..f117d06e9 100644 --- a/src/tl/tl/tlTimer.cc +++ b/src/tl/tl/tlTimer.cc @@ -49,6 +49,12 @@ namespace tl // ------------------------------------------------------------- +#if defined(_MSC_VER) +// FILETIME and Posix use different zero time. This is the difference +// between Jan 1, 1601 and Jan 1, 1970. This value is taken from pthread-win32. +const uint64_t ft_to_epoch_offset = uint64_t (11644473600) * uint64_t (10000000); +#endif + void current_utc_time (struct timespec *ts) { @@ -68,6 +74,8 @@ void current_utc_time (struct timespec *ts) GetSystemTimeAsFileTime (&ft); uint64_t t = (uint64_t (ft.dwHighDateTime) << (sizeof (ft.dwHighDateTime) * 8)) | uint64_t (ft.dwLowDateTime); + t -= ft_to_epoch_offset; + // t is in 100ns units ts->tv_nsec = (t % 10000000) * 100; ts->tv_sec = (t / 10000000); @@ -96,7 +104,10 @@ static int64_t ms_time () FILETIME ft; GetSystemTimeAsFileTime (&ft); + uint64_t t = (uint64_t (ft.dwHighDateTime) << (sizeof (ft.dwHighDateTime) * 8)) | uint64_t (ft.dwLowDateTime); + t -= ft_to_epoch_offset; + // FILETIME uses 100ns resolution, hence divide by 10000 to get ms: return int64_t (t / 10000);