Cleanup usage of kNumNanosecsPerSec like constants in event.cc
Instead rely on TimeDelta constexpr functions to do conversion between
different units of time.
Bug: webrtc:42223979
Change-Id: I7476a2e344303b907a9e297d42bb8fb396fd3aaa
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/401680
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#45202}
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn
index fc3180f..fdf70e2 100644
--- a/rtc_base/BUILD.gn
+++ b/rtc_base/BUILD.gn
@@ -421,7 +421,6 @@
]
deps = [
":checks",
- ":timeutils",
"../api/units:time_delta",
"synchronization:yield_policy",
"system:warn_current_thread_is_deadlocked",
diff --git a/rtc_base/event.cc b/rtc_base/event.cc
index 05f477c..560b036 100644
--- a/rtc_base/event.cc
+++ b/rtc_base/event.cc
@@ -12,14 +12,13 @@
#include <time.h>
-#include <cstdint>
+#include <cstdlib>
#include <optional>
#include "api/units/time_delta.h"
#include "rtc_base/checks.h"
#include "rtc_base/synchronization/yield_policy.h"
#include "rtc_base/system/warn_current_thread_is_deadlocked.h"
-#include "rtc_base/time_utils.h"
#if defined(WEBRTC_WIN)
#include <windows.h>
@@ -119,7 +118,7 @@
namespace {
-timespec GetTimespec(TimeDelta duration_from_now) {
+timespec GetTimespec(TimeDelta from_now) {
timespec ts;
// Get the current time.
@@ -129,20 +128,15 @@
timeval tv;
gettimeofday(&tv, nullptr);
ts.tv_sec = tv.tv_sec;
- ts.tv_nsec = tv.tv_usec * kNumNanosecsPerMicrosec;
+ ts.tv_nsec = TimeDelta::Micros(tv.tv_usec).ns();
#endif
- // Add the specified number of milliseconds to it.
- int64_t microsecs_from_now = duration_from_now.us();
- ts.tv_sec += microsecs_from_now / kNumMicrosecsPerSec;
- ts.tv_nsec +=
- (microsecs_from_now % kNumMicrosecsPerSec) * kNumNanosecsPerMicrosec;
-
- // Normalize.
- if (ts.tv_nsec >= kNumNanosecsPerSec) {
- ts.tv_sec++;
- ts.tv_nsec -= kNumNanosecsPerSec;
- }
+ // Add the specified number of nanoseconds to it.
+ // Use integer division to transfer full seconds to `tv_sec`.
+ // `ns` has type either std::ldiv_t or std::lldiv_t.
+ auto ns = std::div(ts.tv_nsec + from_now.ns(), TimeDelta::Seconds(1).ns());
+ ts.tv_sec += ns.quot;
+ ts.tv_nsec = ns.rem;
return ts;
}