From 301d39a689394c8a7e27143dd747ef985cec0b20 Mon Sep 17 00:00:00 2001 From: "Luc J. Bourhis" Date: Tue, 26 Jan 2016 13:29:52 +0000 Subject: [PATCH] Windows support for tbxx::time_accu --- tbxx/time_accu.hpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tbxx/time_accu.hpp b/tbxx/time_accu.hpp index 1d962f4847..3574c1e802 100644 --- a/tbxx/time_accu.hpp +++ b/tbxx/time_accu.hpp @@ -1,8 +1,60 @@ #ifndef TBXX_TIME_ACCU_HPP #define TBXX_TIME_ACCU_HPP +#ifdef _MSC_VER +#include + +inline void timeradd(timeval *a, timeval *b, timeval *res) { + res->tv_sec = a->tv_sec + b->tv_sec; + res->tv_usec = a->tv_usec + b->tv_usec; + if(res->tv_usec > 1000000) { + ++res->tv_sec; + res->tv_usec -= 1000000; + } +} + +inline void timersub(timeval *a, timeval *b, timeval *res) { + res->tv_sec = a->tv_sec - b->tv_sec; + res->tv_usec = a->tv_usec - b->tv_usec; + if(res->tv_usec < 0) { + --res->tv_sec; + res->tv_usec += 1000000; + } +} + +/* C.f. the following pieces of official Microsoft documentation: + +GetSystemTimeAsFileTime function: +https://msdn.microsoft.com/en-us/library/windows/desktop/ms724397(v=vs.85).aspx + +FILETIME structure: +https://msdn.microsoft.com/en-us/library/windows/desktop/ms724284(v=vs.85).aspx + +Conversion from Windows begining of time to Unix epoch: +http://stackoverflow.com/a/6161842/1428153 + +Note: time zone not supported but we don't need it for the purpose +of time_accu below. +*/ +inline int gettimeofday(struct timeval *tv, void *tz) { + union { + // time since 1 Jan 1601 in 10-th of micro-seconds (usec) + unsigned long long t; + FILETIME ft; + }; + GetSystemTimeAsFileTime (&ft); + t /= 10LL; //in usec + tv->tv_usec = (long)(t % 1000000); + tv->tv_sec = (long)(t / 1000000 - 11644473600LL); + return 0; +} + +#else + #include +#endif + namespace tbxx { struct time_accu