You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
5.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2005 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_TIME_UTILS_H_
  11. #define RTC_BASE_TIME_UTILS_H_
  12. #include <stdint.h>
  13. #include <time.h>
  14. #include <string>
  15. #include "rtc_base/checks.h"
  16. #include "rtc_base/strings/string_builder.h"
  17. #include "rtc_base/system/rtc_export.h"
  18. namespace rtc {
  19. static const int64_t kNumMillisecsPerSec = INT64_C(1000);
  20. static const int64_t kNumMicrosecsPerSec = INT64_C(1000000);
  21. static const int64_t kNumNanosecsPerSec = INT64_C(1000000000);
  22. static const int64_t kNumMicrosecsPerMillisec =
  23. kNumMicrosecsPerSec / kNumMillisecsPerSec;
  24. static const int64_t kNumNanosecsPerMillisec =
  25. kNumNanosecsPerSec / kNumMillisecsPerSec;
  26. static const int64_t kNumNanosecsPerMicrosec =
  27. kNumNanosecsPerSec / kNumMicrosecsPerSec;
  28. // TODO(honghaiz): Define a type for the time value specifically.
  29. class ClockInterface {
  30. public:
  31. virtual ~ClockInterface() {}
  32. virtual int64_t TimeNanos() const = 0;
  33. };
  34. // Sets the global source of time. This is useful mainly for unit tests.
  35. //
  36. // Returns the previously set ClockInterface, or nullptr if none is set.
  37. //
  38. // Does not transfer ownership of the clock. SetClockForTesting(nullptr)
  39. // should be called before the ClockInterface is deleted.
  40. //
  41. // This method is not thread-safe; it should only be used when no other thread
  42. // is running (for example, at the start/end of a unit test, or start/end of
  43. // main()).
  44. //
  45. // TODO(deadbeef): Instead of having functions that access this global
  46. // ClockInterface, we may want to pass the ClockInterface into everything
  47. // that uses it, eliminating the need for a global variable and this function.
  48. RTC_EXPORT ClockInterface* SetClockForTesting(ClockInterface* clock);
  49. // Returns previously set clock, or nullptr if no custom clock is being used.
  50. RTC_EXPORT ClockInterface* GetClockForTesting();
  51. #if defined(WINUWP)
  52. // Synchronizes the current clock based upon an NTP server's epoch in
  53. // milliseconds.
  54. void SyncWithNtp(int64_t time_from_ntp_server_ms);
  55. #endif // defined(WINUWP)
  56. // Returns the actual system time, even if a clock is set for testing.
  57. // Useful for timeouts while using a test clock, or for logging.
  58. int64_t SystemTimeNanos();
  59. int64_t SystemTimeMillis();
  60. // Returns the current time in milliseconds in 32 bits.
  61. uint32_t Time32();
  62. // Returns the current time in milliseconds in 64 bits.
  63. RTC_EXPORT int64_t TimeMillis();
  64. // Deprecated. Do not use this in any new code.
  65. inline int64_t Time() {
  66. return TimeMillis();
  67. }
  68. // Returns the current time in microseconds.
  69. RTC_EXPORT int64_t TimeMicros();
  70. // Returns the current time in nanoseconds.
  71. RTC_EXPORT int64_t TimeNanos();
  72. // Returns a future timestamp, 'elapsed' milliseconds from now.
  73. int64_t TimeAfter(int64_t elapsed);
  74. // Number of milliseconds that would elapse between 'earlier' and 'later'
  75. // timestamps. The value is negative if 'later' occurs before 'earlier'.
  76. int64_t TimeDiff(int64_t later, int64_t earlier);
  77. int32_t TimeDiff32(uint32_t later, uint32_t earlier);
  78. // The number of milliseconds that have elapsed since 'earlier'.
  79. inline int64_t TimeSince(int64_t earlier) {
  80. return TimeMillis() - earlier;
  81. }
  82. // The number of milliseconds that will elapse between now and 'later'.
  83. inline int64_t TimeUntil(int64_t later) {
  84. return later - TimeMillis();
  85. }
  86. class TimestampWrapAroundHandler {
  87. public:
  88. TimestampWrapAroundHandler();
  89. int64_t Unwrap(uint32_t ts);
  90. private:
  91. uint32_t last_ts_;
  92. int64_t num_wrap_;
  93. };
  94. // Convert from tm, which is relative to 1900-01-01 00:00 to number of
  95. // seconds from 1970-01-01 00:00 ("epoch"). Don't return time_t since that
  96. // is still 32 bits on many systems.
  97. int64_t TmToSeconds(const tm& tm);
  98. // Return the number of microseconds since January 1, 1970, UTC.
  99. // Useful mainly when producing logs to be correlated with other
  100. // devices, and when the devices in question all have properly
  101. // synchronized clocks.
  102. //
  103. // Note that this function obeys the system's idea about what the time
  104. // is. It is not guaranteed to be monotonic; it will jump in case the
  105. // system time is changed, e.g., by some other process calling
  106. // settimeofday. Always use rtc::TimeMicros(), not this function, for
  107. // measuring time intervals and timeouts.
  108. int64_t TimeUTCMicros();
  109. // Return the number of milliseconds since January 1, 1970, UTC.
  110. // See above.
  111. int64_t TimeUTCMillis();
  112. // Interval of time from the range [min, max] inclusive.
  113. class IntervalRange {
  114. public:
  115. IntervalRange() : min_(0), max_(0) {}
  116. IntervalRange(int min, int max) : min_(min), max_(max) {
  117. RTC_DCHECK_LE(min, max);
  118. }
  119. int min() const { return min_; }
  120. int max() const { return max_; }
  121. std::string ToString() const {
  122. rtc::StringBuilder ss;
  123. ss << "[" << min_ << "," << max_ << "]";
  124. return ss.Release();
  125. }
  126. bool operator==(const IntervalRange& o) const {
  127. return min_ == o.min_ && max_ == o.max_;
  128. }
  129. bool operator!=(const IntervalRange& o) const { return !operator==(o); }
  130. private:
  131. int min_;
  132. int max_;
  133. };
  134. } // namespace rtc
  135. #endif // RTC_BASE_TIME_UTILS_H_