diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/datetime/base.h | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/datetime/base.h')
-rw-r--r-- | util/datetime/base.h | 268 |
1 files changed, 134 insertions, 134 deletions
diff --git a/util/datetime/base.h b/util/datetime/base.h index 5e902b8f63..3c13e29f10 100644 --- a/util/datetime/base.h +++ b/util/datetime/base.h @@ -1,7 +1,7 @@ #pragma once -#include "systime.h" - +#include "systime.h" + #include <util/str_stl.h> #include <util/system/platform.h> #include <util/system/datetime.h> @@ -9,7 +9,7 @@ #include <util/generic/strbuf.h> #include <util/generic/ylimits.h> #include <util/generic/utility.h> -#include <util/generic/typetraits.h> +#include <util/generic/typetraits.h> #include <util/generic/yexception.h> #include <chrono> @@ -18,20 +18,20 @@ #include <compare> #endif -#include <ctime> -#include <cstdio> +#include <ctime> +#include <cstdio> #include <ratio> - -#include <time.h> - + +#include <time.h> + #ifdef _MSC_VER - #pragma warning(push) - #pragma warning(disable : 4244) // conversion from 'time_t' to 'long', possible loss of data -#endif // _MSC_VER + #pragma warning(push) + #pragma warning(disable : 4244) // conversion from 'time_t' to 'long', possible loss of data +#endif // _MSC_VER // Microseconds since epoch class TInstant; - + // Duration is microseconds. Could be used to store timeouts, for example. class TDuration; @@ -43,21 +43,21 @@ static inline TInstant Now() noexcept; class TDateTimeParseException: public yexception { }; -const int DATE_BUF_LEN = 4 + 2 + 2 + 1; // [YYYYMMDD*] +const int DATE_BUF_LEN = 4 + 2 + 2 + 1; // [YYYYMMDD*] constexpr long seconds(const struct tm& theTm) { - return 60 * (60 * theTm.tm_hour + theTm.tm_min) + theTm.tm_sec; + return 60 * (60 * theTm.tm_hour + theTm.tm_min) + theTm.tm_sec; } void sprint_gm_date(char* buf, time_t when, long* sec = nullptr); -bool sscan_date(const char* date, struct tm& theTm); +bool sscan_date(const char* date, struct tm& theTm); -const int DATE_8601_LEN = 21; // strlen("YYYY-MM-DDThh:mm:ssZ") = 20 + '\0' +const int DATE_8601_LEN = 21; // strlen("YYYY-MM-DDThh:mm:ssZ") = 20 + '\0' -size_t FormatDate8601(char* buf, size_t len, time_t when); +size_t FormatDate8601(char* buf, size_t len, time_t when); -inline void sprint_date8601(char* buf, time_t when) { - buf[FormatDate8601(buf, 64, when)] = 0; +inline void sprint_date8601(char* buf, time_t when) { + buf[FormatDate8601(buf, 64, when)] = 0; } bool ParseISO8601DateTimeDeprecated(const char* date, time_t& utcTime); @@ -79,9 +79,9 @@ bool ParseX509ValidityDateTime(const char* date, time_t& utcTime); bool ParseX509ValidityDateTime(const char* date, size_t dateLen, time_t& utcTime); constexpr long TVdiff(timeval r1, timeval r2) { - return (1000000 * (r2.tv_sec - r1.tv_sec) + (r2.tv_usec - r1.tv_usec)); -} - + return (1000000 * (r2.tv_sec - r1.tv_sec) + (r2.tv_usec - r1.tv_usec)); +} + TString Strftime(const char* format, const struct tm* tm); // Use functions below instead of sprint_date (check IGNIETFERRO-892 for details) @@ -100,7 +100,7 @@ public: protected: constexpr TTimeBase(const TValue& value) noexcept - : Value_(value) + : Value_(value) { } @@ -111,12 +111,12 @@ public: } constexpr TTimeBase(const struct timeval& tv) noexcept - : Value_(tv.tv_sec * (ui64)1000000 + tv.tv_usec) + : Value_(tv.tv_sec * (ui64)1000000 + tv.tv_usec) { } constexpr TValue GetValue() const noexcept { - return Value_; + return Value_; } constexpr double SecondsFloat() const noexcept { @@ -128,25 +128,25 @@ public: } constexpr TValue MicroSeconds() const noexcept { - return Value_; + return Value_; } - + constexpr TValue MilliSeconds() const noexcept { return MicroSeconds() / 1000; } - + constexpr TValue Seconds() const noexcept { return MilliSeconds() / 1000; } - + constexpr TValue Minutes() const noexcept { return Seconds() / 60; } - + constexpr TValue Hours() const noexcept { return Minutes() / 60; } - + constexpr TValue Days() const noexcept { return Hours() / 24; } @@ -154,19 +154,19 @@ public: constexpr TValue NanoSeconds() const noexcept { return MicroSeconds() >= (Max<TValue>() / (TValue)1000) ? Max<TValue>() : MicroSeconds() * (TValue)1000; } - + constexpr ui32 MicroSecondsOfSecond() const noexcept { - return MicroSeconds() % (TValue)1000000; + return MicroSeconds() % (TValue)1000000; } - + constexpr ui32 MilliSecondsOfSecond() const noexcept { - return MicroSecondsOfSecond() / (TValue)1000; + return MicroSecondsOfSecond() / (TValue)1000; } constexpr ui32 NanoSecondsOfSecond() const noexcept { - return MicroSecondsOfSecond() * (TValue)1000; - } - + return MicroSecondsOfSecond() * (TValue)1000; + } + constexpr explicit operator bool() const noexcept { return Value_; } @@ -190,17 +190,17 @@ namespace NDateTimeHelpers { struct TPrecisionHelper<double> { using THighPrecision = double; }; -} +} class TDuration: public TTimeBase<TDuration> { using TBase = TTimeBase<TDuration>; - + private: /** * private construct from microseconds */ constexpr explicit TDuration(TValue value) noexcept - : TBase(value) + : TBase(value) { } @@ -267,13 +267,13 @@ public: /* noexcept(false) as conversion from T might throw, for example FromString("abc") */ template <typename T> static constexpr TDuration MilliSeconds(T ms) noexcept(false) { - return MicroSeconds((ui64)(typename NDateTimeHelpers::TPrecisionHelper<T>::THighPrecision(ms) * 1000)); + return MicroSeconds((ui64)(typename NDateTimeHelpers::TPrecisionHelper<T>::THighPrecision(ms) * 1000)); } using TBase::Days; using TBase::Hours; - using TBase::MicroSeconds; - using TBase::MilliSeconds; + using TBase::MicroSeconds; + using TBase::MilliSeconds; using TBase::Minutes; using TBase::Seconds; @@ -282,11 +282,11 @@ public: constexpr TInstant ToDeadLine(TInstant now) const; static constexpr TDuration Max() noexcept { - return TDuration(::Max<TValue>()); + return TDuration(::Max<TValue>()); } static constexpr TDuration Zero() noexcept { - return TDuration(); + return TDuration(); } /* noexcept(false) as conversion from T might throw, for example FromString("abc") */ @@ -298,48 +298,48 @@ public: static constexpr TDuration Minutes(ui64 m) noexcept { return Seconds(m * 60); } - + static constexpr TDuration Hours(ui64 h) noexcept { return Minutes(h * 60); } - + static constexpr TDuration Days(ui64 d) noexcept { return Hours(d * 24); } /// parses strings like 10s, 15ms, 15.05s, 20us, or just 25 (s). See parser_ut.cpp for details static TDuration Parse(const TStringBuf input); - + static bool TryParse(const TStringBuf input, TDuration& result); // note global Out method is defined for TDuration, so it could be written to IOutputStream as text - - template <class T> + + template <class T> inline TDuration& operator+=(const T& t) noexcept { - return (*this = (*this + t)); - } - - template <class T> + return (*this = (*this + t)); + } + + template <class T> inline TDuration& operator-=(const T& t) noexcept { - return (*this = (*this - t)); - } - - template <class T> + return (*this = (*this - t)); + } + + template <class T> inline TDuration& operator*=(const T& t) noexcept { - return (*this = (*this * t)); - } - - template <class T> + return (*this = (*this * t)); + } + + template <class T> inline TDuration& operator/=(const T& t) noexcept { - return (*this = (*this / t)); - } + return (*this = (*this / t)); + } TString ToString() const; }; Y_DECLARE_PODTYPE(TDuration); - -template <> + +template <> struct THash<TDuration> { size_t operator()(const TDuration& key) const { return THash<TDuration::TValue>()(key.GetValue()); @@ -349,13 +349,13 @@ struct THash<TDuration> { /// TInstant and TDuration are guaranteed to have same precision class TInstant: public TTimeBase<TInstant> { using TBase = TTimeBase<TInstant>; - + private: /** * private construct from microseconds since epoch */ constexpr explicit TInstant(TValue value) noexcept - : TBase(value) + : TBase(value) { } @@ -373,39 +373,39 @@ public: } static inline TInstant Now() { - return TInstant::MicroSeconds(::MicroSeconds()); + return TInstant::MicroSeconds(::MicroSeconds()); } using TBase::Days; using TBase::Hours; - using TBase::MicroSeconds; - using TBase::MilliSeconds; + using TBase::MicroSeconds; + using TBase::MilliSeconds; using TBase::Minutes; using TBase::Seconds; static constexpr TInstant Max() noexcept { - return TInstant(::Max<TValue>()); + return TInstant(::Max<TValue>()); } static constexpr TInstant Zero() noexcept { - return TInstant(); + return TInstant(); } /// us since epoch static constexpr TInstant MicroSeconds(ui64 us) noexcept { return TInstant(us); } - + /// ms since epoch static constexpr TInstant MilliSeconds(ui64 ms) noexcept { return MicroSeconds(ms * 1000); } - + /// seconds since epoch static constexpr TInstant Seconds(ui64 s) noexcept { return MilliSeconds(s * 1000); } - + /// minutes since epoch static constexpr TInstant Minutes(ui64 m) noexcept { return Seconds(m * 60); @@ -422,7 +422,7 @@ public: } constexpr time_t TimeT() const noexcept { - return (time_t)Seconds(); + return (time_t)Seconds(); } inline struct timeval TimeVal() const noexcept { @@ -437,7 +437,7 @@ public: time_t clock = Seconds(); return localtime_r(&clock, tm); } - + inline struct tm* GmTime(struct tm* tm) const noexcept { time_t clock = Seconds(); return GmTimeR(&clock, tm); @@ -510,12 +510,12 @@ public: /// See #TryParseIso8601. static TInstant ParseIso8601(TStringBuf); /// See #TryParseRfc822. - static TInstant ParseRfc822(TStringBuf); + static TInstant ParseRfc822(TStringBuf); /// See #TryParseHttp. static TInstant ParseHttp(TStringBuf); /// See #TryParseX509. static TInstant ParseX509Validity(TStringBuf); - + /// ISO 8601 Representation of Dates and Times /// /// @link https://www.iso.org/standard/40874.html Description of format. @@ -546,20 +546,20 @@ public: static bool TryParseHttpDeprecated(TStringBuf input, TInstant& instant); static bool TryParseX509Deprecated(TStringBuf input, TInstant& instant); - template <class T> + template <class T> inline TInstant& operator+=(const T& t) noexcept { - return (*this = (*this + t)); - } - - template <class T> + return (*this = (*this + t)); + } + + template <class T> inline TInstant& operator-=(const T& t) noexcept { - return (*this = (*this - t)); - } + return (*this = (*this - t)); + } }; Y_DECLARE_PODTYPE(TInstant); - -template <> + +template <> struct THash<TInstant> { size_t operator()(const TInstant& key) const { return THash<TInstant::TValue>()(key.GetValue()); @@ -601,72 +601,72 @@ namespace NPrivate { ::NPrivate::TPrintableLocalTime<true, false> FormatLocalUpToSeconds(TInstant instant); ///@} -template <class S> +template <class S> static constexpr bool operator<(const TTimeBase<S>& l, const TTimeBase<S>& r) noexcept { - return l.GetValue() < r.GetValue(); -} - -template <class S> + return l.GetValue() < r.GetValue(); +} + +template <class S> static constexpr bool operator<=(const TTimeBase<S>& l, const TTimeBase<S>& r) noexcept { - return l.GetValue() <= r.GetValue(); -} - -template <class S> + return l.GetValue() <= r.GetValue(); +} + +template <class S> static constexpr bool operator==(const TTimeBase<S>& l, const TTimeBase<S>& r) noexcept { - return l.GetValue() == r.GetValue(); -} - -template <class S> + return l.GetValue() == r.GetValue(); +} + +template <class S> static constexpr bool operator!=(const TTimeBase<S>& l, const TTimeBase<S>& r) noexcept { - return l.GetValue() != r.GetValue(); -} - -template <class S> + return l.GetValue() != r.GetValue(); +} + +template <class S> static constexpr bool operator>(const TTimeBase<S>& l, const TTimeBase<S>& r) noexcept { - return l.GetValue() > r.GetValue(); -} - -template <class S> + return l.GetValue() > r.GetValue(); +} + +template <class S> static constexpr bool operator>=(const TTimeBase<S>& l, const TTimeBase<S>& r) noexcept { - return l.GetValue() >= r.GetValue(); -} - + return l.GetValue() >= r.GetValue(); +} + namespace NDateTimeHelpers { template <typename T> static constexpr T SumWithSaturation(T a, T b) { static_assert(!std::numeric_limits<T>::is_signed, "expect !std::numeric_limits<T>::is_signed"); - + return Max<T>() - a < b ? Max<T>() : a + b; } template <typename T> static constexpr T DiffWithSaturation(T a, T b) { static_assert(!std::numeric_limits<T>::is_signed, "expect !std::numeric_limits<T>::is_signed"); - + return a < b ? 0 : a - b; } } constexpr TDuration operator-(const TInstant& l, const TInstant& r) noexcept { return TDuration::FromValue(::NDateTimeHelpers::DiffWithSaturation(l.GetValue(), r.GetValue())); -} - +} + constexpr TInstant operator+(const TInstant& i, const TDuration& d) noexcept { return TInstant::FromValue(::NDateTimeHelpers::SumWithSaturation(i.GetValue(), d.GetValue())); -} - +} + constexpr TInstant operator-(const TInstant& i, const TDuration& d) noexcept { return TInstant::FromValue(::NDateTimeHelpers::DiffWithSaturation(i.GetValue(), d.GetValue())); -} - +} + constexpr TDuration operator-(const TDuration& l, const TDuration& r) noexcept { return TDuration::FromValue(::NDateTimeHelpers::DiffWithSaturation(l.GetValue(), r.GetValue())); -} - +} + 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); @@ -771,13 +771,13 @@ constexpr TInstant operator-(const TInstant& l, const std::chrono::duration<T, T return l + (-r); } -template <class T> +template <class T> inline TDuration operator*(TDuration d, T t) noexcept { Y_ASSERT(t >= T()); Y_ASSERT(t == T() || Max<TDuration::TValue>() / t >= d.GetValue()); return TDuration::FromValue(d.GetValue() * t); -} - +} + template <> inline TDuration operator*(TDuration d, double t) noexcept { Y_ASSERT(t >= 0 && MaxFloor<TDuration::TValue>() >= d.GetValue() * t); @@ -797,8 +797,8 @@ inline TDuration operator*(T t, TDuration d) noexcept { template <class T, std::enable_if_t<!std::is_same<TDuration, T>::value, int> = 0> constexpr TDuration operator/(const TDuration& d, const T& t) noexcept { return TDuration::FromValue(d.GetValue() / t); -} - +} + constexpr double operator/(const TDuration& x, const TDuration& y) noexcept { return static_cast<double>(x.GetValue()) / static_cast<double>(y.GetValue()); } @@ -808,16 +808,16 @@ inline TInstant TDuration::ToDeadLine() const { } constexpr TInstant TDuration::ToDeadLine(TInstant now) const { - return now + *this; + return now + *this; } void Sleep(TDuration duration); -void SleepUntil(TInstant instant); +void SleepUntil(TInstant instant); static inline TInstant Now() noexcept { return TInstant::Now(); } #ifdef _MSC_VER - #pragma warning(pop) + #pragma warning(pop) #endif // _MSC_VER |