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.

150 lines
4.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2018 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 API_UNITS_TIMESTAMP_H_
  11. #define API_UNITS_TIMESTAMP_H_
  12. #ifdef UNIT_TEST
  13. #include <ostream> // no-presubmit-check TODO(webrtc:8982)
  14. #endif // UNIT_TEST
  15. #include <string>
  16. #include <type_traits>
  17. #include "api/units/time_delta.h"
  18. #include "rtc_base/checks.h"
  19. namespace webrtc {
  20. // Timestamp represents the time that has passed since some unspecified epoch.
  21. // The epoch is assumed to be before any represented timestamps, this means that
  22. // negative values are not valid. The most notable feature is that the
  23. // difference of two Timestamps results in a TimeDelta.
  24. class Timestamp final : public rtc_units_impl::UnitBase<Timestamp> {
  25. public:
  26. Timestamp() = delete;
  27. template <int64_t seconds>
  28. static constexpr Timestamp Seconds() {
  29. return FromStaticFraction<seconds, 1000000>();
  30. }
  31. template <int64_t ms>
  32. static constexpr Timestamp Millis() {
  33. return FromStaticFraction<ms, 1000>();
  34. }
  35. template <int64_t us>
  36. static constexpr Timestamp Micros() {
  37. return FromStaticValue<us>();
  38. }
  39. template <typename T>
  40. static Timestamp seconds(T seconds) {
  41. static_assert(std::is_arithmetic<T>::value, "");
  42. return FromFraction<1000000>(seconds);
  43. }
  44. template <typename T>
  45. static Timestamp ms(T milliseconds) {
  46. static_assert(std::is_arithmetic<T>::value, "");
  47. return FromFraction<1000>(milliseconds);
  48. }
  49. template <typename T>
  50. static Timestamp us(T microseconds) {
  51. static_assert(std::is_arithmetic<T>::value, "");
  52. return FromValue(microseconds);
  53. }
  54. template <typename T = int64_t>
  55. T seconds() const {
  56. return ToFraction<1000000, T>();
  57. }
  58. template <typename T = int64_t>
  59. T ms() const {
  60. return ToFraction<1000, T>();
  61. }
  62. template <typename T = int64_t>
  63. T us() const {
  64. return ToValue<T>();
  65. }
  66. constexpr int64_t seconds_or(int64_t fallback_value) const {
  67. return ToFractionOr<1000000>(fallback_value);
  68. }
  69. constexpr int64_t ms_or(int64_t fallback_value) const {
  70. return ToFractionOr<1000>(fallback_value);
  71. }
  72. constexpr int64_t us_or(int64_t fallback_value) const {
  73. return ToValueOr(fallback_value);
  74. }
  75. Timestamp operator+(const TimeDelta delta) const {
  76. if (IsPlusInfinity() || delta.IsPlusInfinity()) {
  77. RTC_DCHECK(!IsMinusInfinity());
  78. RTC_DCHECK(!delta.IsMinusInfinity());
  79. return PlusInfinity();
  80. } else if (IsMinusInfinity() || delta.IsMinusInfinity()) {
  81. RTC_DCHECK(!IsPlusInfinity());
  82. RTC_DCHECK(!delta.IsPlusInfinity());
  83. return MinusInfinity();
  84. }
  85. return Timestamp::us(us() + delta.us());
  86. }
  87. Timestamp operator-(const TimeDelta delta) const {
  88. if (IsPlusInfinity() || delta.IsMinusInfinity()) {
  89. RTC_DCHECK(!IsMinusInfinity());
  90. RTC_DCHECK(!delta.IsPlusInfinity());
  91. return PlusInfinity();
  92. } else if (IsMinusInfinity() || delta.IsPlusInfinity()) {
  93. RTC_DCHECK(!IsPlusInfinity());
  94. RTC_DCHECK(!delta.IsMinusInfinity());
  95. return MinusInfinity();
  96. }
  97. return Timestamp::us(us() - delta.us());
  98. }
  99. TimeDelta operator-(const Timestamp other) const {
  100. if (IsPlusInfinity() || other.IsMinusInfinity()) {
  101. RTC_DCHECK(!IsMinusInfinity());
  102. RTC_DCHECK(!other.IsPlusInfinity());
  103. return TimeDelta::PlusInfinity();
  104. } else if (IsMinusInfinity() || other.IsPlusInfinity()) {
  105. RTC_DCHECK(!IsPlusInfinity());
  106. RTC_DCHECK(!other.IsMinusInfinity());
  107. return TimeDelta::MinusInfinity();
  108. }
  109. return TimeDelta::us(us() - other.us());
  110. }
  111. Timestamp& operator-=(const TimeDelta delta) {
  112. *this = *this - delta;
  113. return *this;
  114. }
  115. Timestamp& operator+=(const TimeDelta delta) {
  116. *this = *this + delta;
  117. return *this;
  118. }
  119. private:
  120. friend class rtc_units_impl::UnitBase<Timestamp>;
  121. using UnitBase::UnitBase;
  122. static constexpr bool one_sided = true;
  123. };
  124. std::string ToString(Timestamp value);
  125. inline std::string ToLogString(Timestamp value) {
  126. return ToString(value);
  127. }
  128. #ifdef UNIT_TEST
  129. inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
  130. std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
  131. Timestamp value) {
  132. return stream << ToString(value);
  133. }
  134. #endif // UNIT_TEST
  135. } // namespace webrtc
  136. #endif // API_UNITS_TIMESTAMP_H_