diff options
author | Vlad Yaroslavlev <vladon@vladon.com> | 2022-02-10 16:46:25 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:25 +0300 |
commit | 344ea37b4a345701ab0e67de2266a1c1bd7baf2d (patch) | |
tree | 1a2c5ffcf89eb53ecd79dbc9bc0a195c27404d0c /util/datetime | |
parent | 706b83ed7de5a473436620367af31fc0ceecde07 (diff) | |
download | ydb-344ea37b4a345701ab0e67de2266a1c1bd7baf2d.tar.gz |
Restoring authorship annotation for Vlad Yaroslavlev <vladon@vladon.com>. Commit 2 of 2.
Diffstat (limited to 'util/datetime')
-rw-r--r-- | util/datetime/base.cpp | 18 | ||||
-rw-r--r-- | util/datetime/base.h | 206 | ||||
-rw-r--r-- | util/datetime/base_ut.cpp | 42 | ||||
-rw-r--r-- | util/datetime/cputimer.cpp | 8 | ||||
-rw-r--r-- | util/datetime/cputimer.h | 12 | ||||
-rw-r--r-- | util/datetime/parser.h | 2 | ||||
-rw-r--r-- | util/datetime/systime.cpp | 2 | ||||
-rw-r--r-- | util/datetime/systime.h | 4 | ||||
-rw-r--r-- | util/datetime/uptime.cpp | 104 | ||||
-rw-r--r-- | util/datetime/uptime.h | 16 | ||||
-rw-r--r-- | util/datetime/uptime_ut.cpp | 22 | ||||
-rw-r--r-- | util/datetime/ut/ya.make | 2 |
12 files changed, 219 insertions, 219 deletions
diff --git a/util/datetime/base.cpp b/util/datetime/base.cpp index f0df9b1b40..38ecc3ab96 100644 --- a/util/datetime/base.cpp +++ b/util/datetime/base.cpp @@ -5,17 +5,17 @@ #include <util/stream/mem.h> #include <util/system/compat.h> #include <util/memory/tempbuf.h> -#include <util/generic/string.h> +#include <util/generic/string.h> #include <util/generic/strbuf.h> #include <util/generic/yexception.h> -TString Strftime(const char* format, const struct tm* tm) { +TString Strftime(const char* format, const struct tm* tm) { size_t size = Max<size_t>(strlen(format) * 2 + 1, 107); for (;;) { TTempBuf buf(size); int r = strftime(buf.Data(), buf.Size(), format, tm); if (r != 0) { - return TString(buf.Data(), r); + return TString(buf.Data(), r); } size *= 2; } @@ -173,11 +173,11 @@ void Out<::NPrivate::TPrintableLocalTime<true, true>>(IOutputStream& os, TTypeTr WritePrintableLocalTimeToStream(os, localTime); } -TString TDuration::ToString() const { +TString TDuration::ToString() const { return ::ToString(*this); } -TString TInstant::ToString() const { +TString TInstant::ToString() const { return ::ToString(*this); } @@ -185,20 +185,20 @@ TString TInstant::ToRfc822String() const { return FormatGmTime("%a, %d %b %Y %H:%M:%S GMT"); } -TString TInstant::ToStringUpToSeconds() const { +TString TInstant::ToStringUpToSeconds() const { char buf[64]; auto len = FormatDate8601(buf, sizeof(buf), TimeT()); if (!len) { ythrow yexception() << "TInstant::ToStringUpToSeconds: year does not fit into an integer"; } - return TString(buf, len); + return TString(buf, len); } TString TInstant::ToIsoStringLocal() const { return ::ToString(FormatIsoLocal(*this)); } -TString TInstant::ToStringLocal() const { +TString TInstant::ToStringLocal() const { return ::ToString(FormatLocal(*this)); } @@ -210,7 +210,7 @@ TString TInstant::ToIsoStringLocalUpToSeconds() const { return ::ToString(FormatIsoLocalUpToSeconds(*this)); } -TString TInstant::ToStringLocalUpToSeconds() const { +TString TInstant::ToStringLocalUpToSeconds() const { return ::ToString(FormatLocalUpToSeconds(*this)); } diff --git a/util/datetime/base.h b/util/datetime/base.h index b64b9de711..5e902b8f63 100644 --- a/util/datetime/base.h +++ b/util/datetime/base.h @@ -5,7 +5,7 @@ #include <util/str_stl.h> #include <util/system/platform.h> #include <util/system/datetime.h> -#include <util/generic/string.h> +#include <util/generic/string.h> #include <util/generic/strbuf.h> #include <util/generic/ylimits.h> #include <util/generic/utility.h> @@ -36,7 +36,7 @@ class TInstant; class TDuration; /// Current time -static inline TInstant Now() noexcept; +static inline TInstant Now() noexcept; /// Use Now() method to obtain current time instead of *Seconds() unless you understand what are you doing. @@ -82,7 +82,7 @@ constexpr long TVdiff(timeval r1, timeval r2) { return (1000000 * (r2.tv_sec - r1.tv_sec) + (r2.tv_usec - r1.tv_usec)); } -TString Strftime(const char* format, const struct tm* tm); +TString Strftime(const char* format, const struct tm* tm); // Use functions below instead of sprint_date (check IGNIETFERRO-892 for details) void DateToString(char* buf, const struct tm& theTm); @@ -98,28 +98,28 @@ class TTimeBase { public: using TValue = ui64; -protected: - constexpr TTimeBase(const TValue& value) noexcept +protected: + constexpr TTimeBase(const TValue& value) noexcept : Value_(value) { } -public: - constexpr TTimeBase() noexcept - : Value_(0) - { - } - - constexpr TTimeBase(const struct timeval& tv) noexcept +public: + constexpr TTimeBase() noexcept + : Value_(0) + { + } + + constexpr TTimeBase(const struct timeval& tv) noexcept : Value_(tv.tv_sec * (ui64)1000000 + tv.tv_usec) { } - constexpr TValue GetValue() const noexcept { + constexpr TValue GetValue() const noexcept { return Value_; } - constexpr double SecondsFloat() const noexcept { + constexpr double SecondsFloat() const noexcept { return Value_ * (1 / 1000000.0); } @@ -127,43 +127,43 @@ public: return Value_ * (1 / 1000.0); } - constexpr TValue MicroSeconds() const noexcept { + constexpr TValue MicroSeconds() const noexcept { return Value_; } - constexpr TValue MilliSeconds() const noexcept { + constexpr TValue MilliSeconds() const noexcept { return MicroSeconds() / 1000; } - constexpr TValue Seconds() const noexcept { + constexpr TValue Seconds() const noexcept { return MilliSeconds() / 1000; } - constexpr TValue Minutes() const noexcept { + constexpr TValue Minutes() const noexcept { return Seconds() / 60; } - constexpr TValue Hours() const noexcept { + constexpr TValue Hours() const noexcept { return Minutes() / 60; } - constexpr TValue Days() const noexcept { + constexpr TValue Days() const noexcept { return Hours() / 24; } - constexpr TValue NanoSeconds() const noexcept { + constexpr TValue NanoSeconds() const noexcept { return MicroSeconds() >= (Max<TValue>() / (TValue)1000) ? Max<TValue>() : MicroSeconds() * (TValue)1000; } - constexpr ui32 MicroSecondsOfSecond() const noexcept { + constexpr ui32 MicroSecondsOfSecond() const noexcept { return MicroSeconds() % (TValue)1000000; } - constexpr ui32 MilliSecondsOfSecond() const noexcept { + constexpr ui32 MilliSecondsOfSecond() const noexcept { return MicroSecondsOfSecond() / (TValue)1000; } - constexpr ui32 NanoSecondsOfSecond() const noexcept { + constexpr ui32 NanoSecondsOfSecond() const noexcept { return MicroSecondsOfSecond() * (TValue)1000; } @@ -195,20 +195,20 @@ namespace NDateTimeHelpers { class TDuration: public TTimeBase<TDuration> { using TBase = TTimeBase<TDuration>; -private: - /** - * private construct from microseconds - */ - constexpr explicit TDuration(TValue value) noexcept +private: + /** + * private construct from microseconds + */ + constexpr explicit TDuration(TValue value) noexcept : TBase(value) { } -public: - constexpr TDuration() noexcept { - } - - constexpr TDuration(const struct timeval& tv) noexcept +public: + constexpr TDuration() noexcept { + } + + constexpr TDuration(const struct timeval& tv) noexcept : TBase(tv) { } @@ -256,11 +256,11 @@ public: } } - static constexpr TDuration FromValue(TValue value) noexcept { - return TDuration(value); - } - - static constexpr TDuration MicroSeconds(ui64 us) noexcept { + static constexpr TDuration FromValue(TValue value) noexcept { + return TDuration(value); + } + + static constexpr TDuration MicroSeconds(ui64 us) noexcept { return TDuration(us); } @@ -281,11 +281,11 @@ public: inline TInstant ToDeadLine() const; constexpr TInstant ToDeadLine(TInstant now) const; - static constexpr TDuration Max() noexcept { + static constexpr TDuration Max() noexcept { return TDuration(::Max<TValue>()); } - static constexpr TDuration Zero() noexcept { + static constexpr TDuration Zero() noexcept { return TDuration(); } @@ -295,15 +295,15 @@ public: return MilliSeconds(typename NDateTimeHelpers::TPrecisionHelper<T>::THighPrecision(s) * 1000); } - static constexpr TDuration Minutes(ui64 m) noexcept { + static constexpr TDuration Minutes(ui64 m) noexcept { return Seconds(m * 60); } - static constexpr TDuration Hours(ui64 h) noexcept { + static constexpr TDuration Hours(ui64 h) noexcept { return Minutes(h * 60); } - static constexpr TDuration Days(ui64 d) noexcept { + static constexpr TDuration Days(ui64 d) noexcept { return Hours(d * 24); } @@ -315,26 +315,26 @@ public: // note global Out method is defined for TDuration, so it could be written to IOutputStream as text template <class T> - inline TDuration& operator+=(const T& t) noexcept { + inline TDuration& operator+=(const T& t) noexcept { return (*this = (*this + t)); } template <class T> - inline TDuration& operator-=(const T& t) noexcept { + inline TDuration& operator-=(const T& t) noexcept { return (*this = (*this - t)); } template <class T> - inline TDuration& operator*=(const T& t) noexcept { + inline TDuration& operator*=(const T& t) noexcept { return (*this = (*this * t)); } template <class T> - inline TDuration& operator/=(const T& t) noexcept { + inline TDuration& operator/=(const T& t) noexcept { return (*this = (*this / t)); } - TString ToString() const; + TString ToString() const; }; Y_DECLARE_PODTYPE(TDuration); @@ -350,28 +350,28 @@ struct THash<TDuration> { class TInstant: public TTimeBase<TInstant> { using TBase = TTimeBase<TInstant>; -private: - /** - * private construct from microseconds since epoch - */ +private: + /** + * private construct from microseconds since epoch + */ constexpr explicit TInstant(TValue value) noexcept : TBase(value) { } -public: - constexpr TInstant() noexcept { - } - +public: + constexpr TInstant() noexcept { + } + constexpr TInstant(const struct timeval& tv) noexcept : TBase(tv) { } - static constexpr TInstant FromValue(TValue value) noexcept { - return TInstant(value); - } - + static constexpr TInstant FromValue(TValue value) noexcept { + return TInstant(value); + } + static inline TInstant Now() { return TInstant::MicroSeconds(::MicroSeconds()); } @@ -383,49 +383,49 @@ public: using TBase::Minutes; using TBase::Seconds; - static constexpr TInstant Max() noexcept { + static constexpr TInstant Max() noexcept { return TInstant(::Max<TValue>()); } - static constexpr TInstant Zero() noexcept { + static constexpr TInstant Zero() noexcept { return TInstant(); } /// us since epoch - static constexpr TInstant MicroSeconds(ui64 us) noexcept { + static constexpr TInstant MicroSeconds(ui64 us) noexcept { return TInstant(us); } /// ms since epoch - static constexpr TInstant MilliSeconds(ui64 ms) noexcept { + static constexpr TInstant MilliSeconds(ui64 ms) noexcept { return MicroSeconds(ms * 1000); } /// seconds since epoch - static constexpr TInstant Seconds(ui64 s) noexcept { + static constexpr TInstant Seconds(ui64 s) noexcept { return MilliSeconds(s * 1000); } /// minutes since epoch - static constexpr TInstant Minutes(ui64 m) noexcept { + static constexpr TInstant Minutes(ui64 m) noexcept { return Seconds(m * 60); } /// hours since epoch - static constexpr TInstant Hours(ui64 h) noexcept { + static constexpr TInstant Hours(ui64 h) noexcept { return Minutes(h * 60); } /// days since epoch - static constexpr TInstant Days(ui64 d) noexcept { + static constexpr TInstant Days(ui64 d) noexcept { return Hours(d * 24); } - constexpr time_t TimeT() const noexcept { + constexpr time_t TimeT() const noexcept { return (time_t)Seconds(); } - inline struct timeval TimeVal() const noexcept { + inline struct timeval TimeVal() const noexcept { struct timeval tv; ::Zero(tv); tv.tv_sec = TimeT(); @@ -433,12 +433,12 @@ public: return tv; } - inline struct tm* LocalTime(struct tm* tm) const noexcept { + inline struct tm* LocalTime(struct tm* tm) const noexcept { time_t clock = Seconds(); return localtime_r(&clock, tm); } - inline struct tm* GmTime(struct tm* tm) const noexcept { + inline struct tm* GmTime(struct tm* tm) const noexcept { time_t clock = Seconds(); return GmTimeR(&clock, tm); } @@ -449,7 +449,7 @@ public: * @returns An ISO 8601 formatted string, e.g. '2015-11-21T23:30:27.991669Z'. * @note Global Out method is defined to TInstant, so it can be written as text to IOutputStream. */ - TString ToString() const; + TString ToString() const; /** * Formats the instant using the UTC time zone. @@ -463,7 +463,7 @@ public: * * @returns An ISO 8601 formatted string, e.g. '2015-11-21T23:30:27Z'. */ - TString ToStringUpToSeconds() const; + TString ToStringUpToSeconds() const; /** * Formats the instant using the system time zone, with microsecond precision. @@ -479,7 +479,7 @@ public: * @returns A semi-ISO 8601 formatted string with timezone without colon, * e.g. '2015-11-22T04:30:27.991669+0500'. */ - TString ToStringLocal() const; + TString ToStringLocal() const; /** * Formats the instant using the system time zone. @@ -502,7 +502,7 @@ public: * @returns A semi-ISO 8601 formatted string with timezone without colon, * e.g. '2015-11-22T04:30:27+0500'. */ - TString ToStringLocalUpToSeconds() const; + TString ToStringLocalUpToSeconds() const; TString FormatLocalTime(const char* format) const noexcept; TString FormatGmTime(const char* format) const noexcept; @@ -547,12 +547,12 @@ public: static bool TryParseX509Deprecated(TStringBuf input, TInstant& instant); template <class T> - inline TInstant& operator+=(const T& t) noexcept { + inline TInstant& operator+=(const T& t) noexcept { return (*this = (*this + t)); } template <class T> - inline TInstant& operator-=(const T& t) noexcept { + inline TInstant& operator-=(const T& t) noexcept { return (*this = (*this - t)); } }; @@ -602,32 +602,32 @@ namespace NPrivate { ///@} template <class S> -static constexpr bool operator<(const TTimeBase<S>& l, const TTimeBase<S>& r) noexcept { +static constexpr bool operator<(const TTimeBase<S>& l, const TTimeBase<S>& r) noexcept { return l.GetValue() < r.GetValue(); } template <class S> -static constexpr bool operator<=(const TTimeBase<S>& l, const TTimeBase<S>& r) noexcept { +static constexpr bool operator<=(const TTimeBase<S>& l, const TTimeBase<S>& r) noexcept { return l.GetValue() <= r.GetValue(); } template <class S> -static constexpr bool operator==(const TTimeBase<S>& l, const TTimeBase<S>& r) noexcept { +static constexpr bool operator==(const TTimeBase<S>& l, const TTimeBase<S>& r) noexcept { return l.GetValue() == r.GetValue(); } template <class S> -static constexpr bool operator!=(const TTimeBase<S>& l, const TTimeBase<S>& r) noexcept { +static constexpr bool operator!=(const TTimeBase<S>& l, const TTimeBase<S>& r) noexcept { return l.GetValue() != r.GetValue(); } template <class S> -static constexpr bool operator>(const TTimeBase<S>& l, const TTimeBase<S>& r) noexcept { +static constexpr bool operator>(const TTimeBase<S>& l, const TTimeBase<S>& r) noexcept { return l.GetValue() > r.GetValue(); } template <class S> -static constexpr bool operator>=(const TTimeBase<S>& l, const TTimeBase<S>& r) noexcept { +static constexpr bool operator>=(const TTimeBase<S>& l, const TTimeBase<S>& r) noexcept { return l.GetValue() >= r.GetValue(); } @@ -647,24 +647,24 @@ namespace NDateTimeHelpers { } } -constexpr TDuration operator-(const TInstant& l, const TInstant& r) noexcept { - return TDuration::FromValue(::NDateTimeHelpers::DiffWithSaturation(l.GetValue(), r.GetValue())); +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::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 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::DiffWithSaturation(l.GetValue(), r.GetValue())); } -constexpr TDuration operator+(const TDuration& l, const TDuration& r) noexcept { - return TDuration::FromValue(::NDateTimeHelpers::SumWithSaturation(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> @@ -775,7 +775,7 @@ 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); + return TDuration::FromValue(d.GetValue() * t); } template <> @@ -795,14 +795,14 @@ 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 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()); } -constexpr double operator/(const TDuration& x, const TDuration& y) noexcept { - return static_cast<double>(x.GetValue()) / static_cast<double>(y.GetValue()); -} - inline TInstant TDuration::ToDeadLine() const { return ToDeadLine(TInstant::Now()); } @@ -814,7 +814,7 @@ constexpr TInstant TDuration::ToDeadLine(TInstant now) const { void Sleep(TDuration duration); void SleepUntil(TInstant instant); -static inline TInstant Now() noexcept { +static inline TInstant Now() noexcept { return TInstant::Now(); } diff --git a/util/datetime/base_ut.cpp b/util/datetime/base_ut.cpp index 6699076949..afc3f802eb 100644 --- a/util/datetime/base_ut.cpp +++ b/util/datetime/base_ut.cpp @@ -411,9 +411,9 @@ Y_UNIT_TEST_SUITE(DateTimeTest) { } Y_UNIT_TEST(TestInstantToString) { - UNIT_ASSERT_VALUES_EQUAL(TString("2009-08-06T15:19:06.023455Z"), ToString(TInstant::Seconds(1249571946) + TDuration::MicroSeconds(23455))); - UNIT_ASSERT_VALUES_EQUAL(TString("2009-08-06T15:19:06.023455Z"), (TInstant::Seconds(1249571946) + TDuration::MicroSeconds(23455)).ToString()); - UNIT_ASSERT_VALUES_EQUAL(TString("2009-08-06T15:19:06Z"), (TInstant::Seconds(1249571946) + TDuration::MicroSeconds(23455)).ToStringUpToSeconds()); + UNIT_ASSERT_VALUES_EQUAL(TString("2009-08-06T15:19:06.023455Z"), ToString(TInstant::Seconds(1249571946) + TDuration::MicroSeconds(23455))); + UNIT_ASSERT_VALUES_EQUAL(TString("2009-08-06T15:19:06.023455Z"), (TInstant::Seconds(1249571946) + TDuration::MicroSeconds(23455)).ToString()); + UNIT_ASSERT_VALUES_EQUAL(TString("2009-08-06T15:19:06Z"), (TInstant::Seconds(1249571946) + TDuration::MicroSeconds(23455)).ToStringUpToSeconds()); } Y_UNIT_TEST(TestInstantToRfc822String) { @@ -461,10 +461,10 @@ Y_UNIT_TEST_SUITE(DateTimeTest) { template <class T> void TestTimeUnits() { - T withTime = T::MicroSeconds(1249571946000000L); - T onlyMinutes = T::MicroSeconds(1249571940000000L); - T onlyHours = T::MicroSeconds(1249570800000000L); - T onlyDays = T::MicroSeconds(1249516800000000L); + T withTime = T::MicroSeconds(1249571946000000L); + T onlyMinutes = T::MicroSeconds(1249571940000000L); + T onlyHours = T::MicroSeconds(1249570800000000L); + T onlyDays = T::MicroSeconds(1249516800000000L); ui64 minutes = 20826199; ui64 hours = 347103; ui64 days = 14462; @@ -496,22 +496,22 @@ Y_UNIT_TEST_SUITE(DateTimeTest) { UNIT_ASSERT_EXCEPTION(TDuration::MilliSeconds(FromString(TStringBuf("not a number"))), yexception); UNIT_ASSERT_EXCEPTION(TDuration::Seconds(FromString(TStringBuf("not a number"))), yexception); } - + Y_UNIT_TEST(TestFromValueForTDuration) { - // check that FromValue creates the same TDuration - TDuration d1 = TDuration::MicroSeconds(12345); - TDuration d2 = TDuration::FromValue(d1.GetValue()); - - UNIT_ASSERT_VALUES_EQUAL(d1, d2); - } - + // check that FromValue creates the same TDuration + TDuration d1 = TDuration::MicroSeconds(12345); + TDuration d2 = TDuration::FromValue(d1.GetValue()); + + UNIT_ASSERT_VALUES_EQUAL(d1, d2); + } + Y_UNIT_TEST(TestFromValueForTInstant) { - // check that FromValue creates the same TInstant - TInstant i1 = TInstant::MicroSeconds(12345); - TInstant i2 = TInstant::FromValue(i1.GetValue()); - - UNIT_ASSERT_VALUES_EQUAL(i1, i2); - } + // check that FromValue creates the same TInstant + TInstant i1 = TInstant::MicroSeconds(12345); + TInstant i2 = TInstant::FromValue(i1.GetValue()); + + UNIT_ASSERT_VALUES_EQUAL(i1, i2); + } Y_UNIT_TEST(TestTimeGmDateConversion) { tm time{}; diff --git a/util/datetime/cputimer.cpp b/util/datetime/cputimer.cpp index 08f97a2c7a..516d372c37 100644 --- a/util/datetime/cputimer.cpp +++ b/util/datetime/cputimer.cpp @@ -65,14 +65,14 @@ ui64 TPrecisionTimer::GetCycleCount() const { return ::GetCycleCount() - Start; } -TString FormatCycles(ui64 cycles) { +TString FormatCycles(ui64 cycles) { ui64 milliseconds = cycles / GetCyclesPerMillisecond(); ui32 ms = ui32(milliseconds % 1000); milliseconds /= 1000; ui32 secs = ui32(milliseconds % 60); milliseconds /= 60; ui32 mins = ui32(milliseconds); - TString result; + TString result; sprintf(result, "%" PRIu32 " m %.2" PRIu32 " s %.3" PRIu32 " ms", mins, secs, ms); return result; } @@ -98,11 +98,11 @@ TFuncTimer::TFuncTimer(const char* func) Cerr << "enter " << Func_ << Endl; } -TFuncTimer::~TFuncTimer() { +TFuncTimer::~TFuncTimer() { Cerr << "leave " << Func_ << " -> " << (TInstant::Now() - Start_) << Endl; } -TTimeLogger::TTimeLogger(const TString& message, bool verbose) +TTimeLogger::TTimeLogger(const TString& message, bool verbose) : Message(message) , Verbose(verbose) , OK(false) diff --git a/util/datetime/cputimer.h b/util/datetime/cputimer.h index eae2f535ad..7d38d5bdb3 100644 --- a/util/datetime/cputimer.h +++ b/util/datetime/cputimer.h @@ -3,7 +3,7 @@ #include "base.h" #include <util/system/rusage.h> -#include <util/generic/string.h> +#include <util/generic/string.h> #include <util/stream/str.h> class TTimer { @@ -72,7 +72,7 @@ public: ui64 GetCycleCount() const; }; -TString FormatCycles(ui64 cycles); +TString FormatCycles(ui64 cycles); class TFormattedPrecisionTimer { private: @@ -88,7 +88,7 @@ public: class TFuncTimer { public: TFuncTimer(const char* func); - ~TFuncTimer(); + ~TFuncTimer(); private: const TInstant Start_; @@ -97,7 +97,7 @@ private: class TFakeTimer { public: - inline TFakeTimer(const char* = nullptr) noexcept { + inline TFakeTimer(const char* = nullptr) noexcept { } }; @@ -109,14 +109,14 @@ public: class TTimeLogger { private: - TString Message; + TString Message; bool Verbose; bool OK; time_t Begin; ui64 BeginCycles; public: - TTimeLogger(const TString& message, bool verbose = true); + TTimeLogger(const TString& message, bool verbose = true); ~TTimeLogger(); void SetOK(); diff --git a/util/datetime/parser.h b/util/datetime/parser.h index 007c5a4615..f0c1b4a0c7 100644 --- a/util/datetime/parser.h +++ b/util/datetime/parser.h @@ -28,7 +28,7 @@ struct TDateTimeFields { Year = year; } - bool IsOk() const noexcept { + bool IsOk() const noexcept { if (Year < 1970) return false; if (Month < 1 || Month > 12) diff --git a/util/datetime/systime.cpp b/util/datetime/systime.cpp index d7e5a69b03..6ee7e8fc6e 100644 --- a/util/datetime/systime.cpp +++ b/util/datetime/systime.cpp @@ -132,7 +132,7 @@ struct tm* GmTimeR(const time_t* timer, struct tm* tmbuf) { return tmbuf; } -TString CTimeR(const time_t* timer) { +TString CTimeR(const time_t* timer) { char sTime[32]; sTime[0] = 0; ctime_r(timer, &sTime[0]); diff --git a/util/datetime/systime.h b/util/datetime/systime.h index e64744977c..491d36e802 100644 --- a/util/datetime/systime.h +++ b/util/datetime/systime.h @@ -1,7 +1,7 @@ #pragma once #include <util/system/platform.h> -#include <util/generic/string.h> +#include <util/generic/string.h> #include <ctime> @@ -9,7 +9,7 @@ time_t TimeGM(const struct tm* t); struct tm* GmTimeR(const time_t* timer, struct tm* tmbuf); // safe version of ctime, convinient version of ctime_r -TString CTimeR(const time_t* timer); +TString CTimeR(const time_t* timer); #ifdef _win_ #include <util/system/winint.h> diff --git a/util/datetime/uptime.cpp b/util/datetime/uptime.cpp index 322a229000..12476a94bf 100644 --- a/util/datetime/uptime.cpp +++ b/util/datetime/uptime.cpp @@ -1,56 +1,56 @@ -#include "uptime.h" - -#if defined(_win_) +#include "uptime.h" + +#if defined(_win_) #include <util/system/winint.h> -#elif defined(_linux_) +#elif defined(_linux_) #include <util/stream/file.h> #include <util/string/cast.h> -#elif defined(_darwin_) +#elif defined(_darwin_) #include <sys/sysctl.h> -#endif - -#if defined(_darwin_) -namespace { - TInstant GetBootTime() { - struct timeval timeSinceBoot; - size_t len = sizeof(timeSinceBoot); - int request[2] = {CTL_KERN, KERN_BOOTTIME}; - if (sysctl(request, 2, &timeSinceBoot, &len, nullptr, 0) < 0) { - ythrow yexception() << "cannot get kern.boottime from sysctl"; - } - return TInstant::MicroSeconds(timeSinceBoot.tv_sec * 1'000'000 + timeSinceBoot.tv_usec); - } - - TDuration GetDarwinUptime() { - TInstant beforeNow; - TInstant afterNow; - TInstant now; - - // avoid race when NTP changes machine time between getting Now() and uptime - afterNow = GetBootTime(); - do { - beforeNow = afterNow; - now = Now(); - afterNow = GetBootTime(); - } while (afterNow != beforeNow); - - return now - beforeNow; - } -} -#endif // _darwin_ - -TDuration Uptime() { -#if defined(_win_) - return TDuration::MilliSeconds(GetTickCount64()); -#elif defined(_linux_) - TUnbufferedFileInput in("/proc/uptime"); - TString uptimeStr = in.ReadLine(); - double up, idle; - if (sscanf(uptimeStr.data(), "%lf %lf", &up, &idle) < 2) { - ythrow yexception() << "cannot read values from /proc/uptime"; - } - return TDuration::MilliSeconds(up * 1000.0); -#elif defined(_darwin_) - return GetDarwinUptime(); -#endif -} +#endif + +#if defined(_darwin_) +namespace { + TInstant GetBootTime() { + struct timeval timeSinceBoot; + size_t len = sizeof(timeSinceBoot); + int request[2] = {CTL_KERN, KERN_BOOTTIME}; + if (sysctl(request, 2, &timeSinceBoot, &len, nullptr, 0) < 0) { + ythrow yexception() << "cannot get kern.boottime from sysctl"; + } + return TInstant::MicroSeconds(timeSinceBoot.tv_sec * 1'000'000 + timeSinceBoot.tv_usec); + } + + TDuration GetDarwinUptime() { + TInstant beforeNow; + TInstant afterNow; + TInstant now; + + // avoid race when NTP changes machine time between getting Now() and uptime + afterNow = GetBootTime(); + do { + beforeNow = afterNow; + now = Now(); + afterNow = GetBootTime(); + } while (afterNow != beforeNow); + + return now - beforeNow; + } +} +#endif // _darwin_ + +TDuration Uptime() { +#if defined(_win_) + return TDuration::MilliSeconds(GetTickCount64()); +#elif defined(_linux_) + TUnbufferedFileInput in("/proc/uptime"); + TString uptimeStr = in.ReadLine(); + double up, idle; + if (sscanf(uptimeStr.data(), "%lf %lf", &up, &idle) < 2) { + ythrow yexception() << "cannot read values from /proc/uptime"; + } + return TDuration::MilliSeconds(up * 1000.0); +#elif defined(_darwin_) + return GetDarwinUptime(); +#endif +} diff --git a/util/datetime/uptime.h b/util/datetime/uptime.h index fabf141734..88f0de63da 100644 --- a/util/datetime/uptime.h +++ b/util/datetime/uptime.h @@ -1,8 +1,8 @@ -#pragma once - -#include "base.h" - -/** - * Returns system uptime - */ -TDuration Uptime(); +#pragma once + +#include "base.h" + +/** + * Returns system uptime + */ +TDuration Uptime(); diff --git a/util/datetime/uptime_ut.cpp b/util/datetime/uptime_ut.cpp index 5ffc372784..7f5ecc482c 100644 --- a/util/datetime/uptime_ut.cpp +++ b/util/datetime/uptime_ut.cpp @@ -1,12 +1,12 @@ #include <library/cpp/testing/unittest/registar.h> - -#include "uptime.h" - -Y_UNIT_TEST_SUITE(TestUptimeSuite) { - Y_UNIT_TEST(TestUptime) { - auto t1 = Uptime(); - Sleep(TDuration::MilliSeconds(50)); // typical uptime resolution is 10-16 ms - auto t2 = Uptime(); - UNIT_ASSERT(t2 > t1); - } -} + +#include "uptime.h" + +Y_UNIT_TEST_SUITE(TestUptimeSuite) { + Y_UNIT_TEST(TestUptime) { + auto t1 = Uptime(); + Sleep(TDuration::MilliSeconds(50)); // typical uptime resolution is 10-16 ms + auto t2 = Uptime(); + UNIT_ASSERT(t2 > t1); + } +} diff --git a/util/datetime/ut/ya.make b/util/datetime/ut/ya.make index 9f5b65aeee..c2bc714059 100644 --- a/util/datetime/ut/ya.make +++ b/util/datetime/ut/ya.make @@ -7,7 +7,7 @@ SRCS( datetime/base_ut.cpp datetime/parser_deprecated_ut.cpp datetime/parser_ut.cpp - datetime/uptime_ut.cpp + datetime/uptime_ut.cpp ) INCLUDE(${ARCADIA_ROOT}/util/tests/ya_util_tests.inc) |