Skip to content

Commit

Permalink
Windows support for tbxx::time_accu
Browse files Browse the repository at this point in the history
  • Loading branch information
luc-j-bourhis committed Jan 26, 2016
1 parent 4da5218 commit 301d39a
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tbxx/time_accu.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,60 @@
#ifndef TBXX_TIME_ACCU_HPP
#define TBXX_TIME_ACCU_HPP

#ifdef _MSC_VER
#include <winsock2.h>

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 <sys/time.h>

#endif

namespace tbxx {

struct time_accu
Expand Down

0 comments on commit 301d39a

Please sign in to comment.