monotonic.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma once
  2. #include <util/datetime/base.h>
  3. namespace NMonotonic {
  4. /**
  5. * Returns current monotonic time in microseconds
  6. *
  7. * On Linux uses CLOCK_BOOTTIME under the hood, so it includes time passed
  8. * during suspend and makes it safe for measuring lease times.
  9. */
  10. ui64 GetMonotonicMicroSeconds();
  11. /**
  12. * Similar to TInstant, but measuring monotonic time
  13. *
  14. * On Linux uses CLOCK_BOOTTIME under the hood, so it includes time passed
  15. * during suspend and makes it safe for measuring lease times.
  16. */
  17. class TMonotonic: public TTimeBase<TMonotonic> {
  18. using TBase = TTimeBase<TMonotonic>;
  19. protected:
  20. constexpr explicit TMonotonic(TValue value) noexcept
  21. : TBase(value)
  22. {
  23. }
  24. public:
  25. constexpr TMonotonic() noexcept {
  26. }
  27. static constexpr TMonotonic FromValue(TValue value) noexcept {
  28. return TMonotonic(value);
  29. }
  30. static inline TMonotonic Now() {
  31. return TMonotonic::MicroSeconds(GetMonotonicMicroSeconds());
  32. }
  33. using TBase::Days;
  34. using TBase::Hours;
  35. using TBase::MicroSeconds;
  36. using TBase::MilliSeconds;
  37. using TBase::Minutes;
  38. using TBase::Seconds;
  39. static constexpr TMonotonic Max() noexcept {
  40. return TMonotonic::FromValue(::Max<ui64>());
  41. }
  42. static constexpr TMonotonic Zero() noexcept {
  43. return TMonotonic::FromValue(0);
  44. }
  45. static constexpr TMonotonic MicroSeconds(ui64 us) noexcept {
  46. return TMonotonic::FromValue(TInstant::MicroSeconds(us).GetValue());
  47. }
  48. static constexpr TMonotonic MilliSeconds(ui64 ms) noexcept {
  49. return TMonotonic::FromValue(TInstant::MilliSeconds(ms).GetValue());
  50. }
  51. static constexpr TMonotonic Seconds(ui64 s) noexcept {
  52. return TMonotonic::FromValue(TInstant::Seconds(s).GetValue());
  53. }
  54. static constexpr TMonotonic Minutes(ui64 m) noexcept {
  55. return TMonotonic::FromValue(TInstant::Minutes(m).GetValue());
  56. }
  57. static constexpr TMonotonic Hours(ui64 h) noexcept {
  58. return TMonotonic::FromValue(TInstant::Hours(h).GetValue());
  59. }
  60. static constexpr TMonotonic Days(ui64 d) noexcept {
  61. return TMonotonic::FromValue(TInstant::Days(d).GetValue());
  62. }
  63. template <class T>
  64. inline TMonotonic& operator+=(const T& t) noexcept {
  65. return (*this = (*this + t));
  66. }
  67. template <class T>
  68. inline TMonotonic& operator-=(const T& t) noexcept {
  69. return (*this = (*this - t));
  70. }
  71. };
  72. } // namespace NMonotonic
  73. Y_DECLARE_PODTYPE(NMonotonic::TMonotonic);
  74. namespace std {
  75. template <>
  76. struct hash<NMonotonic::TMonotonic> {
  77. size_t operator()(const NMonotonic::TMonotonic& key) const noexcept {
  78. return hash<NMonotonic::TMonotonic::TValue>()(key.GetValue());
  79. }
  80. };
  81. }
  82. namespace NMonotonic {
  83. constexpr TDuration operator-(const TMonotonic& l, const TMonotonic& r) {
  84. return TInstant::FromValue(l.GetValue()) - TInstant::FromValue(r.GetValue());
  85. }
  86. constexpr TMonotonic operator+(const TMonotonic& l, const TDuration& r) {
  87. TInstant result = TInstant::FromValue(l.GetValue()) + r;
  88. return TMonotonic::FromValue(result.GetValue());
  89. }
  90. constexpr TMonotonic operator-(const TMonotonic& l, const TDuration& r) {
  91. TInstant result = TInstant::FromValue(l.GetValue()) - r;
  92. return TMonotonic::FromValue(result.GetValue());
  93. }
  94. } // namespace NMonotonic
  95. // TODO: remove, alias for compatibility
  96. using TMonotonic = NMonotonic::TMonotonic;