diff options
author | ivanmorozov <ivanmorozov@yandex-team.com> | 2022-11-29 15:52:07 +0300 |
---|---|---|
committer | ivanmorozov <ivanmorozov@yandex-team.com> | 2022-11-29 15:52:07 +0300 |
commit | 8ff1738e8665e5c3a1a328104806bcafbc1bd7ae (patch) | |
tree | 9657e2ea4d923a22e69e954fe11fb77e35bc3271 /library/cpp/time_provider/monotonic.cpp | |
parent | db99cf88226de50e47bd3acdc0ecfe634ce1d828 (diff) | |
download | ydb-8ff1738e8665e5c3a1a328104806bcafbc1bd7ae.tar.gz |
time providers in separated library for future refactoring
Diffstat (limited to 'library/cpp/time_provider/monotonic.cpp')
-rw-r--r-- | library/cpp/time_provider/monotonic.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/library/cpp/time_provider/monotonic.cpp b/library/cpp/time_provider/monotonic.cpp new file mode 100644 index 0000000000..99126080e2 --- /dev/null +++ b/library/cpp/time_provider/monotonic.cpp @@ -0,0 +1,31 @@ +#include "monotonic.h" + +#include <chrono> + +namespace NMonotonic { + +namespace { +// Unfortunately time_since_epoch() is sometimes negative on wine +// Remember initial time point at program start and use offsets from that +std::chrono::steady_clock::time_point MonotonicOffset = std::chrono::steady_clock::now(); +} + +ui64 GetMonotonicMicroSeconds() { + auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - MonotonicOffset).count(); + // Steady clock is supposed to never jump backwards, but it's better to be safe in case of buggy implementations + if (Y_UNLIKELY(microseconds < 0)) { + microseconds = 0; + } + // Add one so we never return zero + return microseconds + 1; +} + +} // namespace TMonotonic + +template<> +void Out<NMonotonic::TMonotonic>( + IOutputStream& o, + NMonotonic::TMonotonic t) +{ + o << t - NMonotonic::TMonotonic::Zero(); +} |