diff options
author | barricade <barricade@yandex-team.ru> | 2022-02-10 16:49:32 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:32 +0300 |
commit | d6a9908bd3435a3571dfcb969988eba7a690802b (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 | |
parent | 4e57e39ab016cac0037c9ed5f9e5c351bbd238d6 (diff) | |
download | ydb-d6a9908bd3435a3571dfcb969988eba7a690802b.tar.gz |
Restoring authorship annotation for <barricade@yandex-team.ru>. Commit 2 of 2.
-rw-r--r-- | library/cpp/timezone_conversion/civil.cpp | 128 | ||||
-rw-r--r-- | library/cpp/timezone_conversion/ut/civil_ut.cpp | 58 |
2 files changed, 93 insertions, 93 deletions
diff --git a/library/cpp/timezone_conversion/civil.cpp b/library/cpp/timezone_conversion/civil.cpp index caab115186..5986318b9a 100644 --- a/library/cpp/timezone_conversion/civil.cpp +++ b/library/cpp/timezone_conversion/civil.cpp @@ -2,73 +2,73 @@ #include <util/stream/output.h> #include <util/stream/format.h> -#include <util/string/ascii.h> - -namespace { - bool TryParseInt(TStringBuf& s, int& dst, size_t maxDigits) { - int res = 0; - size_t i = 0; - while (i < maxDigits && !s.empty() && IsAsciiDigit(s[0])) { - res = res * 10 + (s[0] - '0'); - ++i; - s.Skip(1); - } - if (i == 0) { - return false; - } - dst = res; - return true; - } - +#include <util/string/ascii.h> + +namespace { + bool TryParseInt(TStringBuf& s, int& dst, size_t maxDigits) { + int res = 0; + size_t i = 0; + while (i < maxDigits && !s.empty() && IsAsciiDigit(s[0])) { + res = res * 10 + (s[0] - '0'); + ++i; + s.Skip(1); + } + if (i == 0) { + return false; + } + dst = res; + return true; + } + bool TryParseUTCOffsetTimezone(TStringBuf name, int& offset) { - static constexpr TStringBuf OFFSET_PREFIX = "UTC"; - if (!name.SkipPrefix(OFFSET_PREFIX)) { - return false; - } - if (name.empty()) { - return false; - } - bool negative; - if (name[0] == '+') { - negative = false; - } else if (name[0] == '-') { - negative = true; - } else { - return false; - } - name.Skip(1); - int hour; - int minute = 0; - if (!TryParseInt(name, hour, 2) || hour > 24) { - return false; - } - if (!name.empty()) { - if (name[0] == ':') { - name.Skip(1); - } - if (!TryParseInt(name, minute, 2) || minute >= 60) { - return false; - } - if (!name.empty()) { - return false; - } - } - if (hour == 24 && minute != 0) { - return false; - } - offset = (hour * 60 + minute) * 60; - if (negative) - offset = -offset; - return true; - } -} // anonymous namespace - + static constexpr TStringBuf OFFSET_PREFIX = "UTC"; + if (!name.SkipPrefix(OFFSET_PREFIX)) { + return false; + } + if (name.empty()) { + return false; + } + bool negative; + if (name[0] == '+') { + negative = false; + } else if (name[0] == '-') { + negative = true; + } else { + return false; + } + name.Skip(1); + int hour; + int minute = 0; + if (!TryParseInt(name, hour, 2) || hour > 24) { + return false; + } + if (!name.empty()) { + if (name[0] == ':') { + name.Skip(1); + } + if (!TryParseInt(name, minute, 2) || minute >= 60) { + return false; + } + if (!name.empty()) { + return false; + } + } + if (hour == 24 && minute != 0) { + return false; + } + offset = (hour * 60 + minute) * 60; + if (negative) + offset = -offset; + return true; + } +} // anonymous namespace + namespace NDatetime { TTimeZone GetTimeZone(TStringBuf name) { - int offset; - if (TryParseUTCOffsetTimezone(name, offset)) { - return GetFixedTimeZone(offset); - } + int offset; + if (TryParseUTCOffsetTimezone(name, offset)) { + return GetFixedTimeZone(offset); + } TTimeZone result; if (!cctz::load_time_zone(static_cast<std::string>(name), &result)) { ythrow TInvalidTimezone() << "Failed to load time zone " << name << ", " << result.name(); diff --git a/library/cpp/timezone_conversion/ut/civil_ut.cpp b/library/cpp/timezone_conversion/ut/civil_ut.cpp index b0db9098c7..a21bd4bd7d 100644 --- a/library/cpp/timezone_conversion/ut/civil_ut.cpp +++ b/library/cpp/timezone_conversion/ut/civil_ut.cpp @@ -37,35 +37,35 @@ Y_UNIT_TEST_SUITE(DateTime) { UNIT_ASSERT_VALUES_EQUAL(absTime, NDatetime::Convert(dt2, lax)); UNIT_ASSERT_EXCEPTION(NDatetime::Convert(absTime, "Unknown time zone"), NDatetime::TInvalidTimezone); } - Y_UNIT_TEST(UTCOffsetTimezone) { - NDatetime::TTimeZone lax = NDatetime::GetTimeZone("UTC+12"); - auto lookup = lax.lookup(std::chrono::system_clock::from_time_t(0)); - UNIT_ASSERT_VALUES_EQUAL(12 * 60 * 60, lookup.offset); - lax = NDatetime::GetTimeZone("UTC-10"); - lookup = lax.lookup(std::chrono::system_clock::from_time_t(0)); - UNIT_ASSERT_VALUES_EQUAL(-10 * 60 * 60, lookup.offset); - lax = NDatetime::GetTimeZone("UTC"); - lookup = lax.lookup(std::chrono::system_clock::from_time_t(0)); - UNIT_ASSERT_VALUES_EQUAL(0, lookup.offset); - lax = NDatetime::GetTimeZone("UTC+0"); - lookup = lax.lookup(std::chrono::system_clock::from_time_t(0)); - UNIT_ASSERT_VALUES_EQUAL(0, lookup.offset); - lax = NDatetime::GetTimeZone("UTC-2"); - lookup = lax.lookup(std::chrono::system_clock::from_time_t(0)); - UNIT_ASSERT_VALUES_EQUAL(-2 * 60 * 60, lookup.offset); - lax = NDatetime::GetTimeZone("UTC-00:30"); - lookup = lax.lookup(std::chrono::system_clock::from_time_t(0)); - UNIT_ASSERT_VALUES_EQUAL(-30 * 60, lookup.offset); - lax = NDatetime::GetTimeZone("UTC-0241"); - lookup = lax.lookup(std::chrono::system_clock::from_time_t(0)); - UNIT_ASSERT_VALUES_EQUAL(-(2 * 60 + 41) * 60, lookup.offset); - UNIT_ASSERT_EXCEPTION(NDatetime::GetTimeZone("UTCUnknown"), NDatetime::TInvalidTimezone); - UNIT_ASSERT_EXCEPTION(NDatetime::GetTimeZone("UTC+:"), NDatetime::TInvalidTimezone); - UNIT_ASSERT_EXCEPTION(NDatetime::GetTimeZone("UTC+24:01"), NDatetime::TInvalidTimezone); - UNIT_ASSERT_EXCEPTION(NDatetime::GetTimeZone("UTC+20:"), NDatetime::TInvalidTimezone); - UNIT_ASSERT_EXCEPTION(NDatetime::GetTimeZone("UTC+20:60"), NDatetime::TInvalidTimezone); - UNIT_ASSERT_EXCEPTION(NDatetime::GetTimeZone("UTC+20:30:"), NDatetime::TInvalidTimezone); - } + Y_UNIT_TEST(UTCOffsetTimezone) { + NDatetime::TTimeZone lax = NDatetime::GetTimeZone("UTC+12"); + auto lookup = lax.lookup(std::chrono::system_clock::from_time_t(0)); + UNIT_ASSERT_VALUES_EQUAL(12 * 60 * 60, lookup.offset); + lax = NDatetime::GetTimeZone("UTC-10"); + lookup = lax.lookup(std::chrono::system_clock::from_time_t(0)); + UNIT_ASSERT_VALUES_EQUAL(-10 * 60 * 60, lookup.offset); + lax = NDatetime::GetTimeZone("UTC"); + lookup = lax.lookup(std::chrono::system_clock::from_time_t(0)); + UNIT_ASSERT_VALUES_EQUAL(0, lookup.offset); + lax = NDatetime::GetTimeZone("UTC+0"); + lookup = lax.lookup(std::chrono::system_clock::from_time_t(0)); + UNIT_ASSERT_VALUES_EQUAL(0, lookup.offset); + lax = NDatetime::GetTimeZone("UTC-2"); + lookup = lax.lookup(std::chrono::system_clock::from_time_t(0)); + UNIT_ASSERT_VALUES_EQUAL(-2 * 60 * 60, lookup.offset); + lax = NDatetime::GetTimeZone("UTC-00:30"); + lookup = lax.lookup(std::chrono::system_clock::from_time_t(0)); + UNIT_ASSERT_VALUES_EQUAL(-30 * 60, lookup.offset); + lax = NDatetime::GetTimeZone("UTC-0241"); + lookup = lax.lookup(std::chrono::system_clock::from_time_t(0)); + UNIT_ASSERT_VALUES_EQUAL(-(2 * 60 + 41) * 60, lookup.offset); + UNIT_ASSERT_EXCEPTION(NDatetime::GetTimeZone("UTCUnknown"), NDatetime::TInvalidTimezone); + UNIT_ASSERT_EXCEPTION(NDatetime::GetTimeZone("UTC+:"), NDatetime::TInvalidTimezone); + UNIT_ASSERT_EXCEPTION(NDatetime::GetTimeZone("UTC+24:01"), NDatetime::TInvalidTimezone); + UNIT_ASSERT_EXCEPTION(NDatetime::GetTimeZone("UTC+20:"), NDatetime::TInvalidTimezone); + UNIT_ASSERT_EXCEPTION(NDatetime::GetTimeZone("UTC+20:60"), NDatetime::TInvalidTimezone); + UNIT_ASSERT_EXCEPTION(NDatetime::GetTimeZone("UTC+20:30:"), NDatetime::TInvalidTimezone); + } Y_UNIT_TEST(Format) { NDatetime::TTimeZone lax = NDatetime::GetTimeZone("America/Los_Angeles"); NDatetime::TCivilSecond tp(2013, 1, 2, 3, 4, 5); |