diff --git a/webrtc/base/timeutils.cc b/webrtc/base/timeutils.cc index 0c40c5286e..de3e6afb28 100644 --- a/webrtc/base/timeutils.cc +++ b/webrtc/base/timeutils.cc @@ -28,8 +28,6 @@ #include "webrtc/base/checks.h" #include "webrtc/base/timeutils.h" -#define EFFICIENT_IMPLEMENTATION 1 - namespace rtc { const uint32_t HALF = 0x80000000; @@ -92,108 +90,24 @@ uint64_t TimeMicros() { return static_cast(TimeNanos() / kNumNanosecsPerMicrosec); } -#if defined(WEBRTC_WIN) -static const uint64_t kFileTimeToUnixTimeEpochOffset = 116444736000000000ULL; - -struct timeval { - long tv_sec, tv_usec; // NOLINT -}; - -// Emulate POSIX gettimeofday(). -// Based on breakpad/src/third_party/glog/src/utilities.cc -static int gettimeofday(struct timeval *tv, void *tz) { - // FILETIME is measured in tens of microseconds since 1601-01-01 UTC. - FILETIME ft; - GetSystemTimeAsFileTime(&ft); - - LARGE_INTEGER li; - li.LowPart = ft.dwLowDateTime; - li.HighPart = ft.dwHighDateTime; - - // Convert to seconds and microseconds since Unix time Epoch. - int64_t micros = (li.QuadPart - kFileTimeToUnixTimeEpochOffset) / 10; - tv->tv_sec = static_cast(micros / kNumMicrosecsPerSec); // NOLINT - tv->tv_usec = static_cast(micros % kNumMicrosecsPerSec); // NOLINT - - return 0; -} - -// Emulate POSIX gmtime_r(). -static struct tm *gmtime_r(const time_t *timep, struct tm *result) { - // On Windows, gmtime is thread safe. - struct tm *tm = gmtime(timep); // NOLINT - if (tm == NULL) { - return NULL; - } - *result = *tm; - return result; -} -#endif // WEBRTC_WIN - -void CurrentTmTime(struct tm *tm, int *microseconds) { - struct timeval timeval; - if (gettimeofday(&timeval, NULL) < 0) { - // Incredibly unlikely code path. - timeval.tv_sec = timeval.tv_usec = 0; - } - time_t secs = timeval.tv_sec; - gmtime_r(&secs, tm); - *microseconds = timeval.tv_usec; -} - uint32_t TimeAfter(int32_t elapsed) { RTC_DCHECK_GE(elapsed, 0); RTC_DCHECK_LT(static_cast(elapsed), HALF); return Time() + elapsed; } -bool TimeIsBetween(uint32_t earlier, uint32_t middle, uint32_t later) { - if (earlier <= later) { - return ((earlier <= middle) && (middle <= later)); - } else { - return !((later < middle) && (middle < earlier)); - } -} - bool TimeIsLaterOrEqual(uint32_t earlier, uint32_t later) { -#if EFFICIENT_IMPLEMENTATION int32_t diff = later - earlier; return (diff >= 0 && static_cast(diff) < HALF); -#else - const bool later_or_equal = TimeIsBetween(earlier, later, earlier + HALF); - return later_or_equal; -#endif } bool TimeIsLater(uint32_t earlier, uint32_t later) { -#if EFFICIENT_IMPLEMENTATION int32_t diff = later - earlier; return (diff > 0 && static_cast(diff) < HALF); -#else - const bool earlier_or_equal = TimeIsBetween(later, earlier, later + HALF); - return !earlier_or_equal; -#endif } int32_t TimeDiff(uint32_t later, uint32_t earlier) { -#if EFFICIENT_IMPLEMENTATION return later - earlier; -#else - const bool later_or_equal = TimeIsBetween(earlier, later, earlier + HALF); - if (later_or_equal) { - if (earlier <= later) { - return static_cast(later - earlier); - } else { - return static_cast(later + (UINT32_MAX - earlier) + 1); - } - } else { - if (later <= earlier) { - return -static_cast(earlier - later); - } else { - return -static_cast(earlier + (UINT32_MAX - later) + 1); - } - } -#endif } int64_t TimeDiff64(int64_t later, int64_t earlier) { diff --git a/webrtc/base/timeutils.h b/webrtc/base/timeutils.h index f0269b572f..4af0d9538f 100644 --- a/webrtc/base/timeutils.h +++ b/webrtc/base/timeutils.h @@ -29,9 +29,6 @@ static const int64_t kNumNanosecsPerMillisec = static const int64_t kNumNanosecsPerMicrosec = kNumNanosecsPerSec / kNumMicrosecsPerSec; -// January 1970, in NTP milliseconds. -static const int64_t kJan1970AsNtpMillisecs = INT64_C(2208988800000); - typedef uint32_t TimeStamp; // Returns the current time in milliseconds in 32 bits. @@ -52,16 +49,9 @@ uint64_t TimeMicros(); // Returns the current time in nanoseconds. uint64_t TimeNanos(); -// Stores current time in *tm and microseconds in *microseconds. -void CurrentTmTime(struct tm *tm, int *microseconds); - // Returns a future timestamp, 'elapsed' milliseconds from now. uint32_t TimeAfter(int32_t elapsed); -// Comparisons between time values, which can wrap around. -bool TimeIsBetween(uint32_t earlier, - uint32_t middle, - uint32_t later); // Inclusive bool TimeIsLaterOrEqual(uint32_t earlier, uint32_t later); // Inclusive bool TimeIsLater(uint32_t earlier, uint32_t later); // Exclusive @@ -93,11 +83,6 @@ inline int32_t TimeUntil(uint32_t later) { return TimeDiff(later, Time()); } -// Converts a unix timestamp in nanoseconds to an NTP timestamp in ms. -inline int64_t UnixTimestampNanosecsToNtpMillisecs(int64_t unix_ts_ns) { - return unix_ts_ns / kNumNanosecsPerMillisec + kJan1970AsNtpMillisecs; -} - class TimestampWrapAroundHandler { public: TimestampWrapAroundHandler(); diff --git a/webrtc/base/timeutils_unittest.cc b/webrtc/base/timeutils_unittest.cc index 636c408220..61e41b7c64 100644 --- a/webrtc/base/timeutils_unittest.cc +++ b/webrtc/base/timeutils_unittest.cc @@ -49,19 +49,6 @@ TEST(TimeTest, Comparison) { EXPECT_TRUE( TimeIsLater(ts_now, ts_later)); EXPECT_TRUE( TimeIsLater(ts_earlier, ts_later)); - // Common comparisons - EXPECT_TRUE( TimeIsBetween(ts_earlier, ts_now, ts_later)); - EXPECT_FALSE(TimeIsBetween(ts_earlier, ts_later, ts_now)); - EXPECT_FALSE(TimeIsBetween(ts_now, ts_earlier, ts_later)); - EXPECT_TRUE( TimeIsBetween(ts_now, ts_later, ts_earlier)); - EXPECT_TRUE( TimeIsBetween(ts_later, ts_earlier, ts_now)); - EXPECT_FALSE(TimeIsBetween(ts_later, ts_now, ts_earlier)); - - // Edge cases - EXPECT_TRUE( TimeIsBetween(ts_earlier, ts_earlier, ts_earlier)); - EXPECT_TRUE( TimeIsBetween(ts_earlier, ts_earlier, ts_later)); - EXPECT_TRUE( TimeIsBetween(ts_earlier, ts_later, ts_later)); - // Earlier of two times EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_earlier)); EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_now)); @@ -127,23 +114,6 @@ TEST(TimeTest, BoundaryComparison) { EXPECT_EQ(-100, TimeDiff(ts_earlier, ts_later)); } -TEST(TimeTest, DISABLED_CurrentTmTime) { - struct tm tm; - int microseconds; - - time_t before = ::time(NULL); - CurrentTmTime(&tm, µseconds); - time_t after = ::time(NULL); - - // Assert that 'tm' represents a time between 'before' and 'after'. - // mktime() uses local time, so we have to compensate for that. - time_t local_delta = before - ::mktime(::gmtime(&before)); // NOLINT - time_t t = ::mktime(&tm) + local_delta; - - EXPECT_TRUE(before <= t && t <= after); - EXPECT_TRUE(0 <= microseconds && microseconds < 1000000); -} - TEST(TimeTest, TestTimeDiff64) { int64_t ts_diff = 100; int64_t ts_earlier = rtc::Time64();