timestamp.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. template <typename T>
  27. static constexpr Timestamp Seconds(T value) {
  28. static_assert(std::is_arithmetic<T>::value, "");
  29. return FromFraction(1'000'000, value);
  30. }
  31. template <typename T>
  32. static constexpr Timestamp Millis(T value) {
  33. static_assert(std::is_arithmetic<T>::value, "");
  34. return FromFraction(1'000, value);
  35. }
  36. template <typename T>
  37. static constexpr Timestamp Micros(T value) {
  38. static_assert(std::is_arithmetic<T>::value, "");
  39. return FromValue(value);
  40. }
  41. Timestamp() = delete;
  42. template <typename T = int64_t>
  43. constexpr T seconds() const {
  44. return ToFraction<1000000, T>();
  45. }
  46. template <typename T = int64_t>
  47. constexpr T ms() const {
  48. return ToFraction<1000, T>();
  49. }
  50. template <typename T = int64_t>
  51. constexpr T us() const {
  52. return ToValue<T>();
  53. }
  54. constexpr int64_t seconds_or(int64_t fallback_value) const {
  55. return ToFractionOr<1000000>(fallback_value);
  56. }
  57. constexpr int64_t ms_or(int64_t fallback_value) const {
  58. return ToFractionOr<1000>(fallback_value);
  59. }
  60. constexpr int64_t us_or(int64_t fallback_value) const {
  61. return ToValueOr(fallback_value);
  62. }
  63. constexpr Timestamp operator+(const TimeDelta delta) const {
  64. if (IsPlusInfinity() || delta.IsPlusInfinity()) {
  65. RTC_DCHECK(!IsMinusInfinity());
  66. RTC_DCHECK(!delta.IsMinusInfinity());
  67. return PlusInfinity();
  68. } else if (IsMinusInfinity() || delta.IsMinusInfinity()) {
  69. RTC_DCHECK(!IsPlusInfinity());
  70. RTC_DCHECK(!delta.IsPlusInfinity());
  71. return MinusInfinity();
  72. }
  73. return Timestamp::Micros(us() + delta.us());
  74. }
  75. constexpr Timestamp operator-(const TimeDelta delta) const {
  76. if (IsPlusInfinity() || delta.IsMinusInfinity()) {
  77. RTC_DCHECK(!IsMinusInfinity());
  78. RTC_DCHECK(!delta.IsPlusInfinity());
  79. return PlusInfinity();
  80. } else if (IsMinusInfinity() || delta.IsPlusInfinity()) {
  81. RTC_DCHECK(!IsPlusInfinity());
  82. RTC_DCHECK(!delta.IsMinusInfinity());
  83. return MinusInfinity();
  84. }
  85. return Timestamp::Micros(us() - delta.us());
  86. }
  87. constexpr TimeDelta operator-(const Timestamp other) const {
  88. if (IsPlusInfinity() || other.IsMinusInfinity()) {
  89. RTC_DCHECK(!IsMinusInfinity());
  90. RTC_DCHECK(!other.IsPlusInfinity());
  91. return TimeDelta::PlusInfinity();
  92. } else if (IsMinusInfinity() || other.IsPlusInfinity()) {
  93. RTC_DCHECK(!IsPlusInfinity());
  94. RTC_DCHECK(!other.IsMinusInfinity());
  95. return TimeDelta::MinusInfinity();
  96. }
  97. return TimeDelta::Micros(us() - other.us());
  98. }
  99. constexpr Timestamp& operator-=(const TimeDelta delta) {
  100. *this = *this - delta;
  101. return *this;
  102. }
  103. constexpr Timestamp& operator+=(const TimeDelta delta) {
  104. *this = *this + delta;
  105. return *this;
  106. }
  107. private:
  108. friend class rtc_units_impl::UnitBase<Timestamp>;
  109. using UnitBase::UnitBase;
  110. static constexpr bool one_sided = true;
  111. };
  112. std::string ToString(Timestamp value);
  113. inline std::string ToLogString(Timestamp value) {
  114. return ToString(value);
  115. }
  116. #ifdef UNIT_TEST
  117. inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
  118. std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
  119. Timestamp value) {
  120. return stream << ToString(value);
  121. }
  122. #endif // UNIT_TEST
  123. } // namespace webrtc
  124. #endif // API_UNITS_TIMESTAMP_H_