diff options
author | pechatnov <pechatnov@yandex-team.ru> | 2022-02-10 16:48:57 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:48:57 +0300 |
commit | 132a3640fac343164b858d0a914e020a848a2848 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /util/datetime/base.h | |
parent | 8e9b2f8bbf4a2320f539eef5b85555f42c065425 (diff) | |
download | ydb-132a3640fac343164b858d0a914e020a848a2848.tar.gz |
Restoring authorship annotation for <pechatnov@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'util/datetime/base.h')
-rw-r--r-- | util/datetime/base.h | 308 |
1 files changed, 154 insertions, 154 deletions
diff --git a/util/datetime/base.h b/util/datetime/base.h index a9c890ff3f..5e902b8f63 100644 --- a/util/datetime/base.h +++ b/util/datetime/base.h @@ -12,15 +12,15 @@ #include <util/generic/typetraits.h> #include <util/generic/yexception.h> -#include <chrono> - -#if defined(__cpp_lib_three_way_comparison) - #include <compare> -#endif - +#include <chrono> + +#if defined(__cpp_lib_three_way_comparison) + #include <compare> +#endif + #include <ctime> #include <cstdio> -#include <ratio> +#include <ratio> #include <time.h> @@ -213,49 +213,49 @@ public: { } - /** - * TDuration is compatible with std::chrono::duration: - * it can be constructed and compared with std::chrono::duration. - * But there is two significant and dangerous differencies between them: - * 1) TDuration is never negative and use saturation between 0 and maximum value. - * std::chrono::duration can be negative and can overflow. - * 2) TDuration uses integer number of microseconds. - * std::chrono::duration is flexible, can be integer of floating point, - * can have different precisions. - * So when casted from std::chrono::duration to TDuration value is clamped and rounded. - * In arithmethic operations std::chrono::duration argument is only rounded, - * result is TDuration and it clamped and rounded. - * In comparisons std::chrono::duration argument is rounded. - */ - template <typename T, typename TRatio> - constexpr TDuration(std::chrono::duration<T, TRatio> duration) noexcept { - static_assert( - std::ratio_greater_equal<TRatio, std::micro>::value && - (!std::is_floating_point<T>::value || std::ratio_greater<TRatio, std::micro>::value), - "Extremely likely it is loss of precision, because TDuration stores microseconds. " - "Cast you duration explicitly to microseconds if you really need it."); - - if (duration.count() < 0) { - *this = TDuration::Zero(); // clamp from the bottom - } else { - if -#if !defined(__NVCC__) - constexpr -#endif - /* if [constexpr] */ (std::ratio_greater<TRatio, std::micro>::value || std::is_floating_point<T>::value) { - // clamp from the top - using TCommonDuration = std::chrono::duration<typename std::common_type<T, TValue>::type, TRatio>; - constexpr auto maxDuration = std::chrono::duration<TValue, std::micro>(::Max<TValue>()); - if (std::chrono::duration_cast<TCommonDuration>(duration) >= std::chrono::duration_cast<TCommonDuration>(maxDuration)) { - *this = TDuration::Max(); - return; - } - } - const TValue us = std::chrono::duration_cast<std::chrono::duration<TValue, std::micro>>(duration).count(); - *this = TDuration::MicroSeconds(us); - } - } - + /** + * TDuration is compatible with std::chrono::duration: + * it can be constructed and compared with std::chrono::duration. + * But there is two significant and dangerous differencies between them: + * 1) TDuration is never negative and use saturation between 0 and maximum value. + * std::chrono::duration can be negative and can overflow. + * 2) TDuration uses integer number of microseconds. + * std::chrono::duration is flexible, can be integer of floating point, + * can have different precisions. + * So when casted from std::chrono::duration to TDuration value is clamped and rounded. + * In arithmethic operations std::chrono::duration argument is only rounded, + * result is TDuration and it clamped and rounded. + * In comparisons std::chrono::duration argument is rounded. + */ + template <typename T, typename TRatio> + constexpr TDuration(std::chrono::duration<T, TRatio> duration) noexcept { + static_assert( + std::ratio_greater_equal<TRatio, std::micro>::value && + (!std::is_floating_point<T>::value || std::ratio_greater<TRatio, std::micro>::value), + "Extremely likely it is loss of precision, because TDuration stores microseconds. " + "Cast you duration explicitly to microseconds if you really need it."); + + if (duration.count() < 0) { + *this = TDuration::Zero(); // clamp from the bottom + } else { + if +#if !defined(__NVCC__) + constexpr +#endif + /* if [constexpr] */ (std::ratio_greater<TRatio, std::micro>::value || std::is_floating_point<T>::value) { + // clamp from the top + using TCommonDuration = std::chrono::duration<typename std::common_type<T, TValue>::type, TRatio>; + constexpr auto maxDuration = std::chrono::duration<TValue, std::micro>(::Max<TValue>()); + if (std::chrono::duration_cast<TCommonDuration>(duration) >= std::chrono::duration_cast<TCommonDuration>(maxDuration)) { + *this = TDuration::Max(); + return; + } + } + const TValue us = std::chrono::duration_cast<std::chrono::duration<TValue, std::micro>>(duration).count(); + *this = TDuration::MicroSeconds(us); + } + } + static constexpr TDuration FromValue(TValue value) noexcept { return TDuration(value); } @@ -667,110 +667,110 @@ constexpr TDuration operator+(const TDuration& l, const TDuration& r) noexcept { return TDuration::FromValue(::NDateTimeHelpers::SumWithSaturation(l.GetValue(), r.GetValue())); } -template <typename T, typename TRatio> -constexpr bool operator==(const TDuration& l, const std::chrono::duration<T, TRatio>& r) noexcept { - return r.count() >= 0 && l == TDuration(r); -} - -#if defined(__cpp_lib_three_way_comparison) - -template <typename T, typename TRatio> -constexpr std::strong_ordering operator<=>(const TDuration& l, const std::chrono::duration<T, TRatio>& r) noexcept { - if (r.count() < 0) { - return std::strong_ordering::greater; - } - return l.GetValue() <=> TDuration(r).GetValue(); -} - -#else - -template <typename T, typename TRatio> -constexpr bool operator<(const TDuration& l, const std::chrono::duration<T, TRatio>& r) noexcept { - return r.count() >= 0 && l < TDuration(r); -} - -template <typename T, typename TRatio> -constexpr bool operator<=(const TDuration& l, const std::chrono::duration<T, TRatio>& r) noexcept { - return r.count() >= 0 && l <= TDuration(r); -} - -template <typename T, typename TRatio> -constexpr bool operator!=(const TDuration& l, const std::chrono::duration<T, TRatio>& r) noexcept { - return !(l == r); -} - -template <typename T, typename TRatio> -constexpr bool operator>(const TDuration& l, const std::chrono::duration<T, TRatio>& r) noexcept { - return r.count() < 0 || l > TDuration(r); -} - -template <typename T, typename TRatio> -constexpr bool operator>=(const TDuration& l, const std::chrono::duration<T, TRatio>& r) noexcept { - return r.count() < 0 || l >= TDuration(r); -} - -template <typename T, typename TRatio> -constexpr bool operator<(const std::chrono::duration<T, TRatio>& l, const TDuration& r) noexcept { - return r > l; -} - -template <typename T, typename TRatio> -constexpr bool operator<=(const std::chrono::duration<T, TRatio>& l, const TDuration& r) noexcept { - return r >= l; -} - -template <typename T, typename TRatio> -constexpr bool operator==(const std::chrono::duration<T, TRatio>& l, const TDuration& r) noexcept { - return r == l; -} - -template <typename T, typename TRatio> -constexpr bool operator!=(const std::chrono::duration<T, TRatio>& l, const TDuration& r) noexcept { - return r != l; -} - -template <typename T, typename TRatio> -constexpr bool operator>(const std::chrono::duration<T, TRatio>& l, const TDuration& r) noexcept { - return r < l; -} - -template <typename T, typename TRatio> -constexpr bool operator>=(const std::chrono::duration<T, TRatio>& l, const TDuration& r) noexcept { - return r >= l; -} - -#endif - -template <typename T, typename TRatio> -constexpr TDuration operator+(const TDuration& l, const std::chrono::duration<T, TRatio>& r) noexcept { - return r < r.zero() ? l - TDuration(-r) : l + TDuration(r); -} - -template <typename T, typename TRatio> -constexpr TDuration operator+(const std::chrono::duration<T, TRatio>& l, const TDuration& r) noexcept { - return r + l; -} - -template <typename T, typename TRatio> -constexpr TDuration operator-(const TDuration& l, const std::chrono::duration<T, TRatio>& r) noexcept { - return l + (-r); -} - -template <typename T, typename TRatio> -constexpr TDuration operator-(const std::chrono::duration<T, TRatio>& l, const TDuration& r) noexcept { - return TDuration(l) - r; -} - -template <typename T, typename TRatio> -constexpr TInstant operator+(const TInstant& l, const std::chrono::duration<T, TRatio>& r) noexcept { - return r < r.zero() ? l - TDuration(-r) : l + TDuration(r); -} - -template <typename T, typename TRatio> -constexpr TInstant operator-(const TInstant& l, const std::chrono::duration<T, TRatio>& r) noexcept { - return l + (-r); -} - +template <typename T, typename TRatio> +constexpr bool operator==(const TDuration& l, const std::chrono::duration<T, TRatio>& r) noexcept { + return r.count() >= 0 && l == TDuration(r); +} + +#if defined(__cpp_lib_three_way_comparison) + +template <typename T, typename TRatio> +constexpr std::strong_ordering operator<=>(const TDuration& l, const std::chrono::duration<T, TRatio>& r) noexcept { + if (r.count() < 0) { + return std::strong_ordering::greater; + } + return l.GetValue() <=> TDuration(r).GetValue(); +} + +#else + +template <typename T, typename TRatio> +constexpr bool operator<(const TDuration& l, const std::chrono::duration<T, TRatio>& r) noexcept { + return r.count() >= 0 && l < TDuration(r); +} + +template <typename T, typename TRatio> +constexpr bool operator<=(const TDuration& l, const std::chrono::duration<T, TRatio>& r) noexcept { + return r.count() >= 0 && l <= TDuration(r); +} + +template <typename T, typename TRatio> +constexpr bool operator!=(const TDuration& l, const std::chrono::duration<T, TRatio>& r) noexcept { + return !(l == r); +} + +template <typename T, typename TRatio> +constexpr bool operator>(const TDuration& l, const std::chrono::duration<T, TRatio>& r) noexcept { + return r.count() < 0 || l > TDuration(r); +} + +template <typename T, typename TRatio> +constexpr bool operator>=(const TDuration& l, const std::chrono::duration<T, TRatio>& r) noexcept { + return r.count() < 0 || l >= TDuration(r); +} + +template <typename T, typename TRatio> +constexpr bool operator<(const std::chrono::duration<T, TRatio>& l, const TDuration& r) noexcept { + return r > l; +} + +template <typename T, typename TRatio> +constexpr bool operator<=(const std::chrono::duration<T, TRatio>& l, const TDuration& r) noexcept { + return r >= l; +} + +template <typename T, typename TRatio> +constexpr bool operator==(const std::chrono::duration<T, TRatio>& l, const TDuration& r) noexcept { + return r == l; +} + +template <typename T, typename TRatio> +constexpr bool operator!=(const std::chrono::duration<T, TRatio>& l, const TDuration& r) noexcept { + return r != l; +} + +template <typename T, typename TRatio> +constexpr bool operator>(const std::chrono::duration<T, TRatio>& l, const TDuration& r) noexcept { + return r < l; +} + +template <typename T, typename TRatio> +constexpr bool operator>=(const std::chrono::duration<T, TRatio>& l, const TDuration& r) noexcept { + return r >= l; +} + +#endif + +template <typename T, typename TRatio> +constexpr TDuration operator+(const TDuration& l, const std::chrono::duration<T, TRatio>& r) noexcept { + return r < r.zero() ? l - TDuration(-r) : l + TDuration(r); +} + +template <typename T, typename TRatio> +constexpr TDuration operator+(const std::chrono::duration<T, TRatio>& l, const TDuration& r) noexcept { + return r + l; +} + +template <typename T, typename TRatio> +constexpr TDuration operator-(const TDuration& l, const std::chrono::duration<T, TRatio>& r) noexcept { + return l + (-r); +} + +template <typename T, typename TRatio> +constexpr TDuration operator-(const std::chrono::duration<T, TRatio>& l, const TDuration& r) noexcept { + return TDuration(l) - r; +} + +template <typename T, typename TRatio> +constexpr TInstant operator+(const TInstant& l, const std::chrono::duration<T, TRatio>& r) noexcept { + return r < r.zero() ? l - TDuration(-r) : l + TDuration(r); +} + +template <typename T, typename TRatio> +constexpr TInstant operator-(const TInstant& l, const std::chrono::duration<T, TRatio>& r) noexcept { + return l + (-r); +} + template <class T> inline TDuration operator*(TDuration d, T t) noexcept { Y_ASSERT(t >= T()); |