#include <stdint.h>
#if defined(WEBRTC_POSIX)
#include <sys/time.h>
#endif
#include "rtc_base/checks.h"
#include "rtc_base/numerics/safe_conversions.h"
#include "rtc_base/system_time.h"
#include "rtc_base/time_utils.h"
#if defined(WEBRTC_WIN)
#include "rtc_base/win32.h"
#endif
#if defined(WEBRTC_WIN)
#include <minwinbase.h>
#endif
namespace rtc {
#if defined(WEBRTC_WIN) || defined(WINUWP)
static constexpr uint64_t kFileTimeToUnixTimeEpochOffset =
116444736000000000ULL;
static constexpr uint64_t kFileTimeToMicroSeconds = 10LL;
#endif
ClockInterface* g_clock = …;
ClockInterface* SetClockForTesting(ClockInterface* clock) { … }
ClockInterface* GetClockForTesting() { … }
#if defined(WINUWP)
namespace {
class TimeHelper final {
public:
TimeHelper(const TimeHelper&) = delete;
static void SyncWithNtp(int64_t ntp_server_time_ms) {
auto& singleton = Singleton();
TIME_ZONE_INFORMATION time_zone;
GetTimeZoneInformation(&time_zone);
int64_t time_zone_bias_ns =
rtc::dchecked_cast<int64_t>(time_zone.Bias) * 60 * 1000 * 1000 * 1000;
singleton.app_start_time_ns_ =
(ntp_server_time_ms - kNTPTimeToUnixTimeEpochOffset) * 1000000 -
time_zone_bias_ns;
singleton.UpdateReferenceTime();
}
static int64_t TicksNs() {
auto& singleton = Singleton();
int64_t result = 0;
LARGE_INTEGER qpcnt;
QueryPerformanceCounter(&qpcnt);
result = rtc::dchecked_cast<int64_t>(
(rtc::dchecked_cast<uint64_t>(qpcnt.QuadPart) * 100000 /
rtc::dchecked_cast<uint64_t>(singleton.os_ticks_per_second_)) *
10000);
result = singleton.app_start_time_ns_ + result -
singleton.time_since_os_start_ns_;
return result;
}
private:
TimeHelper() {
TIME_ZONE_INFORMATION time_zone;
GetTimeZoneInformation(&time_zone);
int64_t time_zone_bias_ns =
rtc::dchecked_cast<int64_t>(time_zone.Bias) * 60 * 1000 * 1000 * 1000;
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
LARGE_INTEGER li;
li.HighPart = ft.dwHighDateTime;
li.LowPart = ft.dwLowDateTime;
app_start_time_ns_ = (li.QuadPart - kFileTimeToUnixTimeEpochOffset) * 100 -
time_zone_bias_ns;
UpdateReferenceTime();
}
static TimeHelper& Singleton() {
static TimeHelper singleton;
return singleton;
}
void UpdateReferenceTime() {
LARGE_INTEGER qpfreq;
QueryPerformanceFrequency(&qpfreq);
os_ticks_per_second_ = rtc::dchecked_cast<int64_t>(qpfreq.QuadPart);
LARGE_INTEGER qpcnt;
QueryPerformanceCounter(&qpcnt);
time_since_os_start_ns_ = rtc::dchecked_cast<int64_t>(
(rtc::dchecked_cast<uint64_t>(qpcnt.QuadPart) * 100000 /
rtc::dchecked_cast<uint64_t>(os_ticks_per_second_)) *
10000);
}
private:
static constexpr uint64_t kNTPTimeToUnixTimeEpochOffset = 2208988800000L;
int64_t app_start_time_ns_;
int64_t time_since_os_start_ns_;
int64_t os_ticks_per_second_;
};
}
void SyncWithNtp(int64_t time_from_ntp_server_ms) {
TimeHelper::SyncWithNtp(time_from_ntp_server_ms);
}
int64_t WinUwpSystemTimeNanos() {
return TimeHelper::TicksNs();
}
#endif
int64_t SystemTimeMillis() { … }
int64_t TimeNanos() { … }
uint32_t Time32() { … }
int64_t TimeMillis() { … }
int64_t TimeMicros() { … }
int64_t TimeAfter(int64_t elapsed) { … }
int32_t TimeDiff32(uint32_t later, uint32_t earlier) { … }
int64_t TimeDiff(int64_t later, int64_t earlier) { … }
int64_t TmToSeconds(const tm& tm) { … }
int64_t TimeUTCMicros() { … }
int64_t TimeUTCMillis() { … }
}