diff --git a/libtransmission/log.cc b/libtransmission/log.cc index 82803aa7d..b9a883fc1 100644 --- a/libtransmission/log.cc +++ b/libtransmission/log.cc @@ -127,8 +127,7 @@ void tr_logFreeQueue(tr_log_message* list) char* tr_logGetTimeStr(char* buf, size_t buflen) { - struct timeval tv; - tr_gettimeofday(&tv); + auto const tv = tr_gettimeofday(); time_t const seconds = tv.tv_sec; auto const milliseconds = int(tv.tv_usec / 1000); char msec_str[8]; diff --git a/libtransmission/session.cc b/libtransmission/session.cc index c2e561945..dd8e3196a 100644 --- a/libtransmission/session.cc +++ b/libtransmission/session.cc @@ -676,8 +676,7 @@ static void onNowTimer(evutil_socket_t /*fd*/, short /*what*/, void* vsession) **/ /* schedule the next timer for right after the next second begins */ - struct timeval tv; - tr_gettimeofday(&tv); + auto const tv = tr_gettimeofday(); int constexpr Min = 100; int constexpr Max = 999999; int const usec = std::clamp(int(1000000 - tv.tv_usec), Min, Max); diff --git a/libtransmission/tr-dht.cc b/libtransmission/tr-dht.cc index 408249765..0cc9afdd1 100644 --- a/libtransmission/tr-dht.cc +++ b/libtransmission/tr-dht.cc @@ -815,8 +815,8 @@ int dht_sendto(int sockfd, void const* buf, int len, int flags, struct sockaddr extern "C" int dht_gettimeofday(struct timeval* tv, struct timezone* tz) { TR_ASSERT(tz == nullptr); - - return tr_gettimeofday(tv); + *tv = tr_gettimeofday(); + return 0; } #endif diff --git a/libtransmission/utils.cc b/libtransmission/utils.cc index a992bb5bf..575dc70c4 100644 --- a/libtransmission/utils.cc +++ b/libtransmission/utils.cc @@ -8,6 +8,7 @@ #include /* isdigit() */ #include #include // DBL_DIG +#include #include // localeconv() #include // SIZE_MAX #include // getenv() @@ -20,9 +21,10 @@ #include #ifdef _WIN32 -#include /* WSAStartup() */ -#include /* Sleep(), GetSystemTimeAsFileTime(), GetEnvironmentVariable() */ +#include /* Sleep(), GetEnvironmentVariable() */ + #include /* CommandLineToArgv() */ +#include /* WSAStartup() */ #endif #ifdef HAVE_ICONV @@ -102,40 +104,14 @@ struct tm* tr_localtime_r(time_t const* timep, struct tm* result) #endif } -int tr_gettimeofday(struct timeval* tv) +struct timeval tr_gettimeofday() { -#ifdef _WIN32 - -#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL - - FILETIME ft; - uint64_t tmp = 0; - - if (tv == nullptr) - { - errno = EINVAL; - return -1; - } - - GetSystemTimeAsFileTime(&ft); - tmp |= ft.dwHighDateTime; - tmp <<= 32; - tmp |= ft.dwLowDateTime; - tmp /= 10; /* to microseconds */ - tmp -= DELTA_EPOCH_IN_MICROSECS; - - tv->tv_sec = tmp / 1000000UL; - tv->tv_usec = tmp % 1000000UL; - - return 0; - -#undef DELTA_EPOCH_IN_MICROSECS - -#else - - return gettimeofday(tv, nullptr); - -#endif + auto const d = std::chrono::system_clock::now().time_since_epoch(); + auto const s = std::chrono::duration_cast(d); + auto ret = timeval{}; + ret.tv_sec = s.count(); + ret.tv_usec = std::chrono::duration_cast(d - s).count(); + return ret; } /*** @@ -554,10 +530,8 @@ bool tr_str_has_suffix(char const* str, char const* suffix) uint64_t tr_time_msec() { - struct timeval tv; - - tr_gettimeofday(&tv); - return (uint64_t)tv.tv_sec * 1000 + (tv.tv_usec / 1000); + auto const tv = tr_gettimeofday(); + return uint64_t(tv.tv_sec) * 1000 + (tv.tv_usec / 1000); } void tr_wait_msec(long int msec) diff --git a/libtransmission/utils.h b/libtransmission/utils.h index bc21e6585..24e3c4797 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -425,7 +425,7 @@ struct tm* tr_localtime_r(time_t const* _clock, struct tm* _result); struct tm* tr_gmtime_r(time_t const* _clock, struct tm* _result); /** @brief Portability wrapper for gettimeofday(), with tz argument dropped */ -int tr_gettimeofday(struct timeval* tv); +struct timeval tr_gettimeofday(); /** * @brief move a file