diff options
author | d-dima <d-dima@yandex-team.ru> | 2022-02-10 16:49:31 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:31 +0300 |
commit | 287c6c9d6962bf1bc8eae9a503b4971f31fca365 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 | |
parent | 847c0f5d21ef6f47f68f38f01819e83dfd2681bf (diff) | |
download | ydb-287c6c9d6962bf1bc8eae9a503b4971f31fca365.tar.gz |
Restoring authorship annotation for <d-dima@yandex-team.ru>. Commit 2 of 2.
-rw-r--r-- | library/cpp/timezone_conversion/civil.h | 92 | ||||
-rw-r--r-- | library/cpp/timezone_conversion/ut/civil_ut.cpp | 74 | ||||
-rw-r--r-- | library/cpp/timezone_conversion/ut/convert_ut.cpp | 32 | ||||
-rw-r--r-- | library/cpp/timezone_conversion/ya.make | 4 |
4 files changed, 101 insertions, 101 deletions
diff --git a/library/cpp/timezone_conversion/civil.h b/library/cpp/timezone_conversion/civil.h index 35e2adc04e..0e95b807ed 100644 --- a/library/cpp/timezone_conversion/civil.h +++ b/library/cpp/timezone_conversion/civil.h @@ -107,23 +107,23 @@ namespace NDatetime { */ TTimeZone GetFixedTimeZone(const long offset); - /** Convert civil time from one timezone to another - * @param[in] src is source time with 'from' timezone - * @param[in] from is a initial timezone - * @param[in] from is a destination timezone - * @return a civil time - */ - template <typename T> - T Convert(const T& src, const TTimeZone& from, const TTimeZone& to) { - return cctz::convert(cctz::convert(src, from), to); - } - + /** Convert civil time from one timezone to another + * @param[in] src is source time with 'from' timezone + * @param[in] from is a initial timezone + * @param[in] from is a destination timezone + * @return a civil time + */ + template <typename T> + T Convert(const T& src, const TTimeZone& from, const TTimeZone& to) { + return cctz::convert(cctz::convert(src, from), to); + } + /** Convert absolute time to civil time by rules from timezone. * @param[in] absTime is an absolute time which is used to convert * @param[in] tz is a loaded timezone * @return a civil time - * - * Note: This function doesn't work properly for dates before 1 Jan 1970! + * + * Note: This function doesn't work properly for dates before 1 Jan 1970! */ TCivilSecond Convert(const TInstant& absTime, const TTimeZone& tz); @@ -132,14 +132,14 @@ namespace NDatetime { * @param[in] absTime is an absolute time which is used to convert * @param[in] tzName is a timezone name which will be loaded * @return a civil time - * - * Note: This function doesn't work properly for dates before 1 Jan 1970! + * + * Note: This function doesn't work properly for dates before 1 Jan 1970! */ TCivilSecond Convert(const TInstant& absTime, TStringBuf tzName); /** Convert a civil time to absolute by using rules from timezone - * - * Note: This function doesn't work properly for dates before 1 Jan 1970! + * + * Note: This function doesn't work properly for dates before 1 Jan 1970! */ TInstant Convert(const TCivilSecond& s, const TTimeZone& tz); @@ -302,35 +302,35 @@ namespace NDatetime { CONSTEXPR_M int DaysPerMonth(TYear year, TMonth month) noexcept { return cctz::detail::impl::days_per_month(year, month); } - - CONSTEXPR_M int DaysPerYear(TYear year, TMonth month) noexcept { - return cctz::detail::impl::days_per_year(year, month); - } - - /** Calculate week number for the given date - * @param[in] cd is a current day - * @param[in] usePreviousYear (optional) true if calculate week number from previous year - * - * The week number starts from 1 for the first week, where Thursday exist (see ISO8601), i.e. - * Jan 2021 - * week# mo tu we th fr sa su - * 53 1 2 3 - * 01 4 5 6 7 8 9 10 - * 02 11 ... - * Jan 2020 - * week# mo tu we th fr sa su - * 01 1 2 3 4 5 - * 02 6 7 8 9 10 11... - * - * In case if you received zero value, you may call function again with usePreviousYear=true - * Also you may use usePreviousYear to calculate week difference between two dates in different year - */ - CONSTEXPR_M int GetYearWeek(const TCivilDay& cd, bool usePreviousYear = false) noexcept { - const auto jan1 = NDatetime::GetWeekday(NDatetime::TCivilDay{cd.year() - (usePreviousYear ? 1 : 0), 1, 1}); - const auto daysCount = GetYearDay(cd) + (usePreviousYear ? DaysPerYear(cd.year()-1, 1) : 0); - - return (daysCount + static_cast<int>(jan1) - 1) / 7 + (jan1 == cctz::weekday::monday || jan1 == cctz::weekday::tuesday || jan1 == cctz::weekday::wednesday); - } + + CONSTEXPR_M int DaysPerYear(TYear year, TMonth month) noexcept { + return cctz::detail::impl::days_per_year(year, month); + } + + /** Calculate week number for the given date + * @param[in] cd is a current day + * @param[in] usePreviousYear (optional) true if calculate week number from previous year + * + * The week number starts from 1 for the first week, where Thursday exist (see ISO8601), i.e. + * Jan 2021 + * week# mo tu we th fr sa su + * 53 1 2 3 + * 01 4 5 6 7 8 9 10 + * 02 11 ... + * Jan 2020 + * week# mo tu we th fr sa su + * 01 1 2 3 4 5 + * 02 6 7 8 9 10 11... + * + * In case if you received zero value, you may call function again with usePreviousYear=true + * Also you may use usePreviousYear to calculate week difference between two dates in different year + */ + CONSTEXPR_M int GetYearWeek(const TCivilDay& cd, bool usePreviousYear = false) noexcept { + const auto jan1 = NDatetime::GetWeekday(NDatetime::TCivilDay{cd.year() - (usePreviousYear ? 1 : 0), 1, 1}); + const auto daysCount = GetYearDay(cd) + (usePreviousYear ? DaysPerYear(cd.year()-1, 1) : 0); + + return (daysCount + static_cast<int>(jan1) - 1) / 7 + (jan1 == cctz::weekday::monday || jan1 == cctz::weekday::tuesday || jan1 == cctz::weekday::wednesday); + } } #include "civil-inl.h" diff --git a/library/cpp/timezone_conversion/ut/civil_ut.cpp b/library/cpp/timezone_conversion/ut/civil_ut.cpp index 7e21462f5e..a21bd4bd7d 100644 --- a/library/cpp/timezone_conversion/ut/civil_ut.cpp +++ b/library/cpp/timezone_conversion/ut/civil_ut.cpp @@ -117,41 +117,41 @@ Y_UNIT_TEST_SUITE(DateTime) { GetCivilDiff(TCivilSecond(2017, 10, 1), TCivilSecond(2017, 9, 31), ECivilUnit::Month), TCivilDiff(0, ECivilUnit::Month)); } - - Y_UNIT_TEST(TestYearWeekNmb) { - - // YEAR 2021 - start from Friday, first dates (1-3) will have week# 0 - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 1, 1}), 0); - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 1, 2}), 0); - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 1, 3}), 0); - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 1, 4}), 1); - - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 1, 1}, true), 53); - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 1, 2}, true), 53); - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 1, 3}, true), 53); - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 1, 4}, true), 54); - - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 2, 28}), 8); - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 2, 29}), 9); // <- this is invalid date, should be normalized to March 1 - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 3, 1}), 9); - - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 12, 26}), 51); - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 12, 27}), 52); - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 12, 31}), 52); - - // YEAR 2020 - start from Wednesday, all dates start from week# 1 - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2020, 1, 1}), 1); - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2020, 1, 5}), 1); - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2020, 1, 6}), 2); - - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2020, 2, 28}), 9); - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2020, 2, 29}), 9); - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2020, 3, 1}), 9); - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2020, 3, 2}), 10); - - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2020, 12, 31}), 53); - - // Max possible delta - calcuate week # for 31 Dec 2021 from 1 Jan 2020 - UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 12, 31}, true), 105); - } + + Y_UNIT_TEST(TestYearWeekNmb) { + + // YEAR 2021 - start from Friday, first dates (1-3) will have week# 0 + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 1, 1}), 0); + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 1, 2}), 0); + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 1, 3}), 0); + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 1, 4}), 1); + + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 1, 1}, true), 53); + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 1, 2}, true), 53); + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 1, 3}, true), 53); + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 1, 4}, true), 54); + + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 2, 28}), 8); + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 2, 29}), 9); // <- this is invalid date, should be normalized to March 1 + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 3, 1}), 9); + + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 12, 26}), 51); + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 12, 27}), 52); + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 12, 31}), 52); + + // YEAR 2020 - start from Wednesday, all dates start from week# 1 + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2020, 1, 1}), 1); + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2020, 1, 5}), 1); + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2020, 1, 6}), 2); + + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2020, 2, 28}), 9); + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2020, 2, 29}), 9); + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2020, 3, 1}), 9); + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2020, 3, 2}), 10); + + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2020, 12, 31}), 53); + + // Max possible delta - calcuate week # for 31 Dec 2021 from 1 Jan 2020 + UNIT_ASSERT_VALUES_EQUAL(NDatetime::GetYearWeek(NDatetime::TCivilDay{2021, 12, 31}, true), 105); + } } diff --git a/library/cpp/timezone_conversion/ut/convert_ut.cpp b/library/cpp/timezone_conversion/ut/convert_ut.cpp index e1a045034a..bbf9e9b826 100644 --- a/library/cpp/timezone_conversion/ut/convert_ut.cpp +++ b/library/cpp/timezone_conversion/ut/convert_ut.cpp @@ -134,22 +134,22 @@ TEST(TimeZoneConversion, Local) { EXPECT_EQ(nowAbsolute.Seconds(), ToAbsoluteTime(nowCivilLocal, local).Seconds()); } -TEST(TimeZoneConversion, BeforeEpoch) { - { - //NOTE: This test will not work because NDatetime::Convert() with TInstant does not work properly for dates before 1/1/1970 - NDatetime::TCivilSecond civilTime = NDatetime::TCivilSecond{1969, 12, 1, 0, 0, 0}; - TInstant absTime = NDatetime::Convert(civilTime, NDatetime::GetUtcTimeZone()); - NDatetime::TCivilSecond civilTime2 = NDatetime::Convert(absTime, NDatetime::GetUtcTimeZone()); - EXPECT_NE(civilTime2, civilTime); // ERROR. Must be EXPECT_EQ, but Convert() functions with TInstant doesnot wotk properly for dates before EPOCH - } - - // Right test - NDatetime::TCivilSecond civilTime = NDatetime::TCivilSecond{1969, 12, 1, 0, 0, 0}; - NDatetime::TCivilSecond civilTime2 = Convert<NDatetime::TCivilSecond>(civilTime, NDatetime::GetUtcTimeZone(), NDatetime::GetUtcTimeZone()); - EXPECT_EQ(civilTime2, civilTime); - -} - +TEST(TimeZoneConversion, BeforeEpoch) { + { + //NOTE: This test will not work because NDatetime::Convert() with TInstant does not work properly for dates before 1/1/1970 + NDatetime::TCivilSecond civilTime = NDatetime::TCivilSecond{1969, 12, 1, 0, 0, 0}; + TInstant absTime = NDatetime::Convert(civilTime, NDatetime::GetUtcTimeZone()); + NDatetime::TCivilSecond civilTime2 = NDatetime::Convert(absTime, NDatetime::GetUtcTimeZone()); + EXPECT_NE(civilTime2, civilTime); // ERROR. Must be EXPECT_EQ, but Convert() functions with TInstant doesnot wotk properly for dates before EPOCH + } + + // Right test + NDatetime::TCivilSecond civilTime = NDatetime::TCivilSecond{1969, 12, 1, 0, 0, 0}; + NDatetime::TCivilSecond civilTime2 = Convert<NDatetime::TCivilSecond>(civilTime, NDatetime::GetUtcTimeZone(), NDatetime::GetUtcTimeZone()); + EXPECT_EQ(civilTime2, civilTime); + +} + TEST(TimeZoneConversion, InvalidTimeZone) { EXPECT_THROW(GetTimeZone("Europe/Mscow"), yexception); EXPECT_THROW(GetTimeZone(""), yexception); diff --git a/library/cpp/timezone_conversion/ya.make b/library/cpp/timezone_conversion/ya.make index 6e49f565a8..f99ebe73ee 100644 --- a/library/cpp/timezone_conversion/ya.make +++ b/library/cpp/timezone_conversion/ya.make @@ -18,5 +18,5 @@ SRCS( GENERATE_ENUM_SERIALIZATION(civil.h) END() - -RECURSE_FOR_TESTS(ut) + +RECURSE_FOR_TESTS(ut) |