#include <folly/portability/SysTime.h>
#ifdef _WIN32
#include <cstdint>
extern "C" {
int gettimeofday(timeval* tv, folly_port_struct_timezone*) {
constexpr auto posixWinFtOffset = 116444736000000000ULL;
if (tv) {
FILETIME ft;
ULARGE_INTEGER lft;
GetSystemTimeAsFileTime(&ft);
lft.HighPart = ft.dwHighDateTime;
lft.LowPart = ft.dwLowDateTime;
uint64_t ns = lft.QuadPart;
tv->tv_usec = (long)((ns / 10ULL) % 1000000ULL);
tv->tv_sec = (long)((ns - posixWinFtOffset) / 10000000ULL);
}
return 0;
}
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;
}
}
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;
}
}
}
#endif