refactor: use std::chrono not OS-specific syscalls (#2554)

This commit is contained in:
Charles Kerr 2022-01-31 19:02:59 -06:00 committed by GitHub
parent 5db993d348
commit b645c4cfc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 46 deletions

View File

@ -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];

View File

@ -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);

View File

@ -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

View File

@ -8,6 +8,7 @@
#include <cctype> /* isdigit() */
#include <cerrno>
#include <cfloat> // DBL_DIG
#include <chrono>
#include <clocale> // localeconv()
#include <cstdint> // SIZE_MAX
#include <cstdlib> // getenv()
@ -20,9 +21,10 @@
#include <vector>
#ifdef _WIN32
#include <ws2tcpip.h> /* WSAStartup() */
#include <windows.h> /* Sleep(), GetSystemTimeAsFileTime(), GetEnvironmentVariable() */
#include <windows.h> /* Sleep(), GetEnvironmentVariable() */
#include <shellapi.h> /* CommandLineToArgv() */
#include <ws2tcpip.h> /* 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<std::chrono::seconds>(d);
auto ret = timeval{};
ret.tv_sec = s.count();
ret.tv_usec = std::chrono::duration_cast<std::chrono::microseconds>(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)

View File

@ -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