diff options
author | hippskill <hippskill@yandex-team.ru> | 2022-02-10 16:50:12 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:50:12 +0300 |
commit | bd077d09a08e659195376ee97dc24728bb554246 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /util/datetime | |
parent | 6362c4a2681cc317ffd22633d773f02de0d13697 (diff) | |
download | ydb-bd077d09a08e659195376ee97dc24728bb554246.tar.gz |
Restoring authorship annotation for <hippskill@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'util/datetime')
-rw-r--r-- | util/datetime/base.cpp | 82 | ||||
-rw-r--r-- | util/datetime/base.h | 18 | ||||
-rw-r--r-- | util/datetime/base_ut.cpp | 194 |
3 files changed, 147 insertions, 147 deletions
diff --git a/util/datetime/base.cpp b/util/datetime/base.cpp index 6061b445d7..38ecc3ab96 100644 --- a/util/datetime/base.cpp +++ b/util/datetime/base.cpp @@ -250,55 +250,55 @@ void sprint_gm_date(char* buf, time_t when, long* sec) { struct tm theTm; ::Zero(theTm); GmTimeR(&when, &theTm); - DateToString(buf, theTm); + DateToString(buf, theTm); if (sec) { *sec = seconds(theTm); } } -void DateToString(char* buf, const struct tm& theTm) { - Y_ENSURE(0 <= theTm.tm_year + 1900 && theTm.tm_year + 1900 <= 9999, "invalid year " + ToString(theTm.tm_year + 1900) + ", year should be in range [0, 9999]"); - - sprintf(buf, "%04d%02d%02d", theTm.tm_year + 1900, theTm.tm_mon + 1, theTm.tm_mday); -} - -void DateToString(char* buf, time_t when, long* sec) { - struct tm theTm; - localtime_r(&when, &theTm); - - DateToString(buf, theTm); - - if (sec) { - *sec = seconds(theTm); - } -} - -TString DateToString(const struct tm& theTm) { - char buf[DATE_BUF_LEN]; - DateToString(buf, theTm); - return buf; -} - -TString DateToString(time_t when, long* sec) { - char buf[DATE_BUF_LEN]; - DateToString(buf, when, sec); - return buf; -} - -TString YearToString(const struct tm& theTm) { - Y_ENSURE(0 <= theTm.tm_year + 1900 && theTm.tm_year + 1900 <= 9999, "invalid year " + ToString(theTm.tm_year + 1900) + ", year should be in range [0, 9999]"); +void DateToString(char* buf, const struct tm& theTm) { + Y_ENSURE(0 <= theTm.tm_year + 1900 && theTm.tm_year + 1900 <= 9999, "invalid year " + ToString(theTm.tm_year + 1900) + ", year should be in range [0, 9999]"); + + sprintf(buf, "%04d%02d%02d", theTm.tm_year + 1900, theTm.tm_mon + 1, theTm.tm_mday); +} + +void DateToString(char* buf, time_t when, long* sec) { + struct tm theTm; + localtime_r(&when, &theTm); + + DateToString(buf, theTm); + + if (sec) { + *sec = seconds(theTm); + } +} + +TString DateToString(const struct tm& theTm) { + char buf[DATE_BUF_LEN]; + DateToString(buf, theTm); + return buf; +} + +TString DateToString(time_t when, long* sec) { + char buf[DATE_BUF_LEN]; + DateToString(buf, when, sec); + return buf; +} + +TString YearToString(const struct tm& theTm) { + Y_ENSURE(0 <= theTm.tm_year + 1900 && theTm.tm_year + 1900 <= 9999, "invalid year " + ToString(theTm.tm_year + 1900) + ", year should be in range [0, 9999]"); char buf[16]; sprintf(buf, "%04d", theTm.tm_year + 1900); return buf; -} - -TString YearToString(time_t when) { - struct tm theTm; - localtime_r(&when, &theTm); - - return YearToString(theTm); -} - +} + +TString YearToString(time_t when) { + struct tm theTm; + localtime_r(&when, &theTm); + + return YearToString(theTm); +} + bool sscan_date(const char* date, struct tm& theTm) { int year, mon, mday; if (sscanf(date, "%4d%2d%2d", &year, &mon, &mday) != 3) { diff --git a/util/datetime/base.h b/util/datetime/base.h index d1cf52c926..5e902b8f63 100644 --- a/util/datetime/base.h +++ b/util/datetime/base.h @@ -84,15 +84,15 @@ constexpr long TVdiff(timeval r1, timeval r2) { 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); -void DateToString(char* buf, time_t when, long* sec = nullptr); -TString DateToString(const struct tm& theTm); -TString DateToString(time_t when, long* sec = nullptr); -// Year in format "YYYY", throws an exception if year not in range [0, 9999] -TString YearToString(const struct tm& theTm); -TString YearToString(time_t when); - +// Use functions below instead of sprint_date (check IGNIETFERRO-892 for details) +void DateToString(char* buf, const struct tm& theTm); +void DateToString(char* buf, time_t when, long* sec = nullptr); +TString DateToString(const struct tm& theTm); +TString DateToString(time_t when, long* sec = nullptr); +// Year in format "YYYY", throws an exception if year not in range [0, 9999] +TString YearToString(const struct tm& theTm); +TString YearToString(time_t when); + template <class S> class TTimeBase { public: diff --git a/util/datetime/base_ut.cpp b/util/datetime/base_ut.cpp index 783a2636dc..afc3f802eb 100644 --- a/util/datetime/base_ut.cpp +++ b/util/datetime/base_ut.cpp @@ -46,108 +46,108 @@ namespace { } } } - + Y_UNIT_TEST_SUITE(TestSprintDate) { Y_UNIT_TEST(Year9999) { - struct tm t; - t.tm_year = 9999 - 1900; - t.tm_mday = 1; - t.tm_mon = 10; - - char buf[DATE_BUF_LEN]; - DateToString(buf, t); - - TString expectedDate = "99991101"; - - UNIT_ASSERT_VALUES_EQUAL(expectedDate, ToString(buf)); - } + struct tm t; + t.tm_year = 9999 - 1900; + t.tm_mday = 1; + t.tm_mon = 10; + + char buf[DATE_BUF_LEN]; + DateToString(buf, t); + + TString expectedDate = "99991101"; + + UNIT_ASSERT_VALUES_EQUAL(expectedDate, ToString(buf)); + } Y_UNIT_TEST(YearAfter9999) { - struct tm t; - t.tm_year = 123456 - 1900; - t.tm_mday = 1; - t.tm_mon = 10; - - char buf[DATE_BUF_LEN]; - UNIT_ASSERT_EXCEPTION(DateToString(buf, t), yexception); - } + struct tm t; + t.tm_year = 123456 - 1900; + t.tm_mday = 1; + t.tm_mon = 10; + + char buf[DATE_BUF_LEN]; + UNIT_ASSERT_EXCEPTION(DateToString(buf, t), yexception); + } Y_UNIT_TEST(SmallYear) { - struct tm t; - t.tm_year = 0 - 1900; - t.tm_mday = 1; - t.tm_mon = 10; - - char buf[DATE_BUF_LEN]; - DateToString(buf, t); - - const TString expectedDate = TString("00001101"); - - UNIT_ASSERT_VALUES_EQUAL(expectedDate, ToString(buf)); - } + struct tm t; + t.tm_year = 0 - 1900; + t.tm_mday = 1; + t.tm_mon = 10; + + char buf[DATE_BUF_LEN]; + DateToString(buf, t); + + const TString expectedDate = TString("00001101"); + + UNIT_ASSERT_VALUES_EQUAL(expectedDate, ToString(buf)); + } Y_UNIT_TEST(SmallYearAndMonth) { - struct tm t; - t.tm_year = 99 - 1900; - t.tm_mday = 1; - t.tm_mon = 0; - - char buf[DATE_BUF_LEN]; - DateToString(buf, t); - - const TString expectedDate = TString("00990101"); - - UNIT_ASSERT_VALUES_EQUAL(expectedDate, ToString(buf)); - } - Y_UNIT_TEST(FromZeroTimestamp) { - const time_t timestamp = 0; - - char buf[DATE_BUF_LEN]; - DateToString(buf, timestamp); - - const TString expectedDate = TString("19700101"); - - UNIT_ASSERT_VALUES_EQUAL(expectedDate, ToString(buf)); - } - Y_UNIT_TEST(FromTimestamp) { - const time_t timestamp = 1524817858; - - char buf[DATE_BUF_LEN]; - DateToString(buf, timestamp); - - const TString expectedDate = TString("20180427"); - - UNIT_ASSERT_VALUES_EQUAL(expectedDate, ToString(buf)); - } - Y_UNIT_TEST(FromTimestampAsTString) { - const time_t timestamp = 1524817858; - - const TString expectedDate = TString("20180427"); - - UNIT_ASSERT_VALUES_EQUAL(expectedDate, DateToString(timestamp)); - } - Y_UNIT_TEST(YearToString) { - struct tm t; - t.tm_year = 99 - 1900; - t.tm_mday = 1; - t.tm_mon = 0; - - TString expectedYear = TString("0099"); - - UNIT_ASSERT_VALUES_EQUAL(expectedYear, YearToString(t)); - } - Y_UNIT_TEST(YearToStringBigYear) { - struct tm t; - t.tm_year = 123456 - 1900; - t.tm_mday = 1; - t.tm_mon = 0; - - UNIT_ASSERT_EXCEPTION(YearToString(t), yexception); - } - Y_UNIT_TEST(YearToStringAsTimestamp) { - const time_t timestamp = 1524817858; - - const TString expectedYear = TString("2018"); - - UNIT_ASSERT_VALUES_EQUAL(expectedYear, YearToString(timestamp)); - } + struct tm t; + t.tm_year = 99 - 1900; + t.tm_mday = 1; + t.tm_mon = 0; + + char buf[DATE_BUF_LEN]; + DateToString(buf, t); + + const TString expectedDate = TString("00990101"); + + UNIT_ASSERT_VALUES_EQUAL(expectedDate, ToString(buf)); + } + Y_UNIT_TEST(FromZeroTimestamp) { + const time_t timestamp = 0; + + char buf[DATE_BUF_LEN]; + DateToString(buf, timestamp); + + const TString expectedDate = TString("19700101"); + + UNIT_ASSERT_VALUES_EQUAL(expectedDate, ToString(buf)); + } + Y_UNIT_TEST(FromTimestamp) { + const time_t timestamp = 1524817858; + + char buf[DATE_BUF_LEN]; + DateToString(buf, timestamp); + + const TString expectedDate = TString("20180427"); + + UNIT_ASSERT_VALUES_EQUAL(expectedDate, ToString(buf)); + } + Y_UNIT_TEST(FromTimestampAsTString) { + const time_t timestamp = 1524817858; + + const TString expectedDate = TString("20180427"); + + UNIT_ASSERT_VALUES_EQUAL(expectedDate, DateToString(timestamp)); + } + Y_UNIT_TEST(YearToString) { + struct tm t; + t.tm_year = 99 - 1900; + t.tm_mday = 1; + t.tm_mon = 0; + + TString expectedYear = TString("0099"); + + UNIT_ASSERT_VALUES_EQUAL(expectedYear, YearToString(t)); + } + Y_UNIT_TEST(YearToStringBigYear) { + struct tm t; + t.tm_year = 123456 - 1900; + t.tm_mday = 1; + t.tm_mon = 0; + + UNIT_ASSERT_EXCEPTION(YearToString(t), yexception); + } + Y_UNIT_TEST(YearToStringAsTimestamp) { + const time_t timestamp = 1524817858; + + const TString expectedYear = TString("2018"); + + UNIT_ASSERT_VALUES_EQUAL(expectedYear, YearToString(timestamp)); + } } Y_UNIT_TEST_SUITE(TDateTimeTest) { |