time_utils.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "rtc_base/checks.h"
  15. #include "rtc_base/system/rtc_export.h"
  16. namespace rtc {
  17. static const int64_t kNumMillisecsPerSec = INT64_C(1000);
  18. static const int64_t kNumMicrosecsPerSec = INT64_C(1000000);
  19. static const int64_t kNumNanosecsPerSec = INT64_C(1000000000);
  20. static const int64_t kNumMicrosecsPerMillisec =
  21. kNumMicrosecsPerSec / kNumMillisecsPerSec;
  22. static const int64_t kNumNanosecsPerMillisec =
  23. kNumNanosecsPerSec / kNumMillisecsPerSec;
  24. static const int64_t kNumNanosecsPerMicrosec =
  25. kNumNanosecsPerSec / kNumMicrosecsPerSec;
  26. // TODO(honghaiz): Define a type for the time value specifically.
  27. class ClockInterface {
  28. public:
  29. virtual ~ClockInterface() {}
  30. virtual int64_t TimeNanos() const = 0;
  31. };
  32. // Sets the global source of time. This is useful mainly for unit tests.
  33. //
  34. // Returns the previously set ClockInterface, or nullptr if none is set.
  35. //
  36. // Does not transfer ownership of the clock. SetClockForTesting(nullptr)
  37. // should be called before the ClockInterface is deleted.
  38. //
  39. // This method is not thread-safe; it should only be used when no other thread
  40. // is running (for example, at the start/end of a unit test, or start/end of
  41. // main()).
  42. //
  43. // TODO(deadbeef): Instead of having functions that access this global
  44. // ClockInterface, we may want to pass the ClockInterface into everything
  45. // that uses it, eliminating the need for a global variable and this function.
  46. RTC_EXPORT ClockInterface* SetClockForTesting(ClockInterface* clock);
  47. // Returns previously set clock, or nullptr if no custom clock is being used.
  48. RTC_EXPORT ClockInterface* GetClockForTesting();
  49. #if defined(WINUWP)
  50. // Synchronizes the current clock based upon an NTP server's epoch in
  51. // milliseconds.
  52. void SyncWithNtp(int64_t time_from_ntp_server_ms);
  53. #endif // defined(WINUWP)
  54. // Returns the actual system time, even if a clock is set for testing.
  55. // Useful for timeouts while using a test clock, or for logging.
  56. int64_t SystemTimeNanos();
  57. int64_t SystemTimeMillis();
  58. // Returns the current time in milliseconds in 32 bits.
  59. uint32_t Time32();
  60. // Returns the current time in milliseconds in 64 bits.
  61. RTC_EXPORT int64_t TimeMillis();
  62. // Deprecated. Do not use this in any new code.
  63. inline int64_t Time() {
  64. return TimeMillis();
  65. }
  66. // Returns the current time in microseconds.
  67. RTC_EXPORT int64_t TimeMicros();
  68. // Returns the current time in nanoseconds.
  69. RTC_EXPORT int64_t TimeNanos();
  70. // Returns a future timestamp, 'elapsed' milliseconds from now.
  71. int64_t TimeAfter(int64_t elapsed);
  72. // Number of milliseconds that would elapse between 'earlier' and 'later'
  73. // timestamps. The value is negative if 'later' occurs before 'earlier'.
  74. int64_t TimeDiff(int64_t later, int64_t earlier);
  75. int32_t TimeDiff32(uint32_t later, uint32_t earlier);
  76. // The number of milliseconds that have elapsed since 'earlier'.
  77. inline int64_t TimeSince(int64_t earlier) {
  78. return TimeMillis() - earlier;
  79. }
  80. // The number of milliseconds that will elapse between now and 'later'.
  81. inline int64_t TimeUntil(int64_t later) {
  82. return later - TimeMillis();
  83. }
  84. class TimestampWrapAroundHandler {
  85. public:
  86. TimestampWrapAroundHandler();
  87. int64_t Unwrap(uint32_t ts);
  88. private:
  89. uint32_t last_ts_;
  90. int64_t num_wrap_;
  91. };
  92. // Convert from tm, which is relative to 1900-01-01 00:00 to number of
  93. // seconds from 1970-01-01 00:00 ("epoch"). Don't return time_t since that
  94. // is still 32 bits on many systems.
  95. int64_t TmToSeconds(const tm& tm);
  96. // Return the number of microseconds since January 1, 1970, UTC.
  97. // Useful mainly when producing logs to be correlated with other
  98. // devices, and when the devices in question all have properly
  99. // synchronized clocks.
  100. //
  101. // Note that this function obeys the system's idea about what the time
  102. // is. It is not guaranteed to be monotonic; it will jump in case the
  103. // system time is changed, e.g., by some other process calling
  104. // settimeofday. Always use rtc::TimeMicros(), not this function, for
  105. // measuring time intervals and timeouts.
  106. int64_t TimeUTCMicros();
  107. // Return the number of milliseconds since January 1, 1970, UTC.
  108. // See above.
  109. int64_t TimeUTCMillis();
  110. } // namespace rtc
  111. #endif // RTC_BASE_TIME_UTILS_H_