aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/timezone_conversion
diff options
context:
space:
mode:
authormoroznastya <moroznastya@yandex-team.com>2023-12-05 21:56:41 +0300
committermoroznastya <moroznastya@yandex-team.com>2023-12-05 22:39:17 +0300
commitfaf9f5de6a6799aa2e80c7e8077ceca4670daa4f (patch)
treea11ceb11c82442398934e3771b488361e0729412 /library/cpp/timezone_conversion
parentca072a4c932bc2df166fb17f03eb2481c43f948d (diff)
downloadydb-faf9f5de6a6799aa2e80c7e8077ceca4670daa4f.tar.gz
parsing promos
Diffstat (limited to 'library/cpp/timezone_conversion')
-rw-r--r--library/cpp/timezone_conversion/civil.cpp8
-rw-r--r--library/cpp/timezone_conversion/civil.h13
-rw-r--r--library/cpp/timezone_conversion/ut/civil_ut.cpp21
3 files changed, 38 insertions, 4 deletions
diff --git a/library/cpp/timezone_conversion/civil.cpp b/library/cpp/timezone_conversion/civil.cpp
index 5986318b9a..0a9b6acaa6 100644
--- a/library/cpp/timezone_conversion/civil.cpp
+++ b/library/cpp/timezone_conversion/civil.cpp
@@ -25,6 +25,12 @@ namespace {
if (!name.SkipPrefix(OFFSET_PREFIX)) {
return false;
}
+ return NDatetime::TryParseOffset(name, offset);
+ }
+} // anonymous namespace
+
+namespace NDatetime {
+ bool TryParseOffset(TStringBuf name, int& offset) {
if (name.empty()) {
return false;
}
@@ -61,9 +67,7 @@ namespace {
offset = -offset;
return true;
}
-} // anonymous namespace
-namespace NDatetime {
TTimeZone GetTimeZone(TStringBuf name) {
int offset;
if (TryParseUTCOffsetTimezone(name, offset)) {
diff --git a/library/cpp/timezone_conversion/civil.h b/library/cpp/timezone_conversion/civil.h
index 0e95b807ed..5f331b3358 100644
--- a/library/cpp/timezone_conversion/civil.h
+++ b/library/cpp/timezone_conversion/civil.h
@@ -101,6 +101,15 @@ namespace NDatetime {
TTimeZone GetTimeZone(TStringBuf name);
/**
+ *Helper for get timezone offset from timezone string
+ * Examples:
+ * "+01:30" -> 5400
+ * "-10" -> -36000
+ * "-0200" -> -7200
+ */
+ bool TryParseOffset(TStringBuf name, int& offset);
+
+ /**
* Returns a time zone that is a fixed offset (seconds east) from UTC.
* Note: If the absolute value of the offset is greater than 24 hours
* you'll get UTC (i.e., zero offset) instead.
@@ -108,7 +117,7 @@ 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] 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
@@ -323,7 +332,7 @@ namespace NDatetime {
* 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
+ * 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});
diff --git a/library/cpp/timezone_conversion/ut/civil_ut.cpp b/library/cpp/timezone_conversion/ut/civil_ut.cpp
index a21bd4bd7d..f91935ac49 100644
--- a/library/cpp/timezone_conversion/ut/civil_ut.cpp
+++ b/library/cpp/timezone_conversion/ut/civil_ut.cpp
@@ -66,6 +66,27 @@ Y_UNIT_TEST_SUITE(DateTime) {
UNIT_ASSERT_EXCEPTION(NDatetime::GetTimeZone("UTC+20:60"), NDatetime::TInvalidTimezone);
UNIT_ASSERT_EXCEPTION(NDatetime::GetTimeZone("UTC+20:30:"), NDatetime::TInvalidTimezone);
}
+ Y_UNIT_TEST(ParseOffset) {
+ int offset;
+ UNIT_ASSERT(!NDatetime::TryParseOffset("Unknown", offset));
+ UNIT_ASSERT(!NDatetime::TryParseOffset("+:", offset));
+ UNIT_ASSERT(!NDatetime::TryParseOffset("+24:01", offset));
+ UNIT_ASSERT(!NDatetime::TryParseOffset("+20:", offset));
+ UNIT_ASSERT(!NDatetime::TryParseOffset("+20:60", offset));
+ UNIT_ASSERT(!NDatetime::TryParseOffset("+20:30:", offset));
+ UNIT_ASSERT(NDatetime::TryParseOffset("+03", offset));
+ UNIT_ASSERT_VALUES_EQUAL(offset, 10800);
+ UNIT_ASSERT(NDatetime::TryParseOffset("-10", offset));
+ UNIT_ASSERT_VALUES_EQUAL(offset, -36000);
+ UNIT_ASSERT(NDatetime::TryParseOffset("+01:00", offset));
+ UNIT_ASSERT_VALUES_EQUAL(offset, 3600);
+ UNIT_ASSERT(NDatetime::TryParseOffset("-11:30", offset));
+ UNIT_ASSERT_VALUES_EQUAL(offset, -41400);
+ UNIT_ASSERT(NDatetime::TryParseOffset("+0130", offset));
+ UNIT_ASSERT_VALUES_EQUAL(offset, 5400);
+ UNIT_ASSERT(NDatetime::TryParseOffset("-0200", offset));
+ UNIT_ASSERT_VALUES_EQUAL(offset, -7200);
+ }
Y_UNIT_TEST(Format) {
NDatetime::TTimeZone lax = NDatetime::GetTimeZone("America/Los_Angeles");
NDatetime::TCivilSecond tp(2013, 1, 2, 3, 4, 5);