diff options
author | ndnuriev <ndnuriev@yandex-team.ru> | 2022-02-10 16:49:45 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:45 +0300 |
commit | 3659e5861440ac98d0380de7698601ad14655a7a (patch) | |
tree | a8cbee25ef3f89582ff526208677df7d14c75c98 /util/datetime/parser.h | |
parent | bfd0aa05cabedcab6f4c4d36948dc7581e476782 (diff) | |
download | ydb-3659e5861440ac98d0380de7698601ad14655a7a.tar.gz |
Restoring authorship annotation for <ndnuriev@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'util/datetime/parser.h')
-rw-r--r-- | util/datetime/parser.h | 266 |
1 files changed, 133 insertions, 133 deletions
diff --git a/util/datetime/parser.h b/util/datetime/parser.h index f0c1b4a0c7..386ec84822 100644 --- a/util/datetime/parser.h +++ b/util/datetime/parser.h @@ -7,7 +7,7 @@ struct TDateTimeFields { TDateTimeFields() { Zero(*this); - ZoneOffsetMinutes = 0; + ZoneOffsetMinutes = 0; Hour = 0; } @@ -155,140 +155,140 @@ struct TDurationParser { bool ParsePart(const char* input, size_t len); TDuration GetResult(TDuration defaultValue) const; }; - -/** -Depcrecated cause of default hour offset (+4 hours) -@see IGNIETFERRO-823 -*/ -struct TDateTimeFieldsDeprecated { - TDateTimeFieldsDeprecated() { - Zero(*this); - ZoneOffsetMinutes = (i32)TDuration::Hours(4).Minutes(); // legacy code - Hour = 11; - } - - ui32 Year; - ui32 Month; // 1..12 - ui32 Day; // 1 .. 31 - ui32 Hour; // 0 .. 23 - ui32 Minute; // 0 .. 59 - ui32 Second; // 0 .. 60 - ui32 MicroSecond; // 0 .. 999999 - i32 ZoneOffsetMinutes; - - void SetLooseYear(ui32 year) { - if (year < 60) - year += 100; - if (year < 160) - year += 1900; - Year = year; - } - - bool IsOk() const noexcept { - if (Year < 1970) - return false; - if (Month < 1 || Month > 12) - return false; - - unsigned int maxMonthDay = 31; - if (Month == 4 || Month == 6 || Month == 9 || Month == 11) { - maxMonthDay = 30; - } else if (Month == 2) { - if (Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)) - // leap year - maxMonthDay = 29; - else - maxMonthDay = 28; - } - if (Day > maxMonthDay) - return false; - - if (Hour > 23) - return false; - - if (Minute > 59) - return false; - - if (Second > 60) - return false; - - if (MicroSecond > 999999) - return false; - - if (Year == 1970 && Month == 1 && Day == 1) { - if ((i64)(3600 * Hour + 60 * Minute + Second) < (60 * ZoneOffsetMinutes)) - return false; - } - - return true; - } - - TInstant ToInstant(TInstant defaultValue) const { - time_t tt = ToTimeT(-1); - if (tt == -1) - return defaultValue; - return TInstant::Seconds(tt) + TDuration::MicroSeconds(MicroSecond); - } - - time_t ToTimeT(time_t defaultValue) const { - if (!IsOk()) - return defaultValue; - struct tm tm; - Zero(tm); - tm.tm_year = Year - 1900; - tm.tm_mon = Month - 1; - tm.tm_mday = Day; - tm.tm_hour = Hour; - tm.tm_min = Minute; - tm.tm_sec = Second; - time_t tt = TimeGM(&tm); - if (tt == -1) - return defaultValue; - return tt - ZoneOffsetMinutes * 60; - } -}; - -class TDateTimeParserBaseDeprecated { -public: - const TDateTimeFieldsDeprecated& GetDateTimeFields() const { - return DateTimeFields; - } - -protected: - TDateTimeFieldsDeprecated DateTimeFields; - int cs; //for ragel - int Sign; - int I; - int Dc; - -protected: - TDateTimeParserBaseDeprecated() - : DateTimeFields() - , cs(0) - , Sign(0) - , I(0xDEADBEEF) // to guarantee unittest break if ragel code is incorrect - , Dc(0xDEADBEEF) - { - } - - inline TInstant GetResult(int firstFinalState, TInstant defaultValue) const { - if (cs < firstFinalState) - return defaultValue; - return DateTimeFields.ToInstant(defaultValue); - } -}; - + +/** +Depcrecated cause of default hour offset (+4 hours) +@see IGNIETFERRO-823 +*/ +struct TDateTimeFieldsDeprecated { + TDateTimeFieldsDeprecated() { + Zero(*this); + ZoneOffsetMinutes = (i32)TDuration::Hours(4).Minutes(); // legacy code + Hour = 11; + } + + ui32 Year; + ui32 Month; // 1..12 + ui32 Day; // 1 .. 31 + ui32 Hour; // 0 .. 23 + ui32 Minute; // 0 .. 59 + ui32 Second; // 0 .. 60 + ui32 MicroSecond; // 0 .. 999999 + i32 ZoneOffsetMinutes; + + void SetLooseYear(ui32 year) { + if (year < 60) + year += 100; + if (year < 160) + year += 1900; + Year = year; + } + + bool IsOk() const noexcept { + if (Year < 1970) + return false; + if (Month < 1 || Month > 12) + return false; + + unsigned int maxMonthDay = 31; + if (Month == 4 || Month == 6 || Month == 9 || Month == 11) { + maxMonthDay = 30; + } else if (Month == 2) { + if (Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)) + // leap year + maxMonthDay = 29; + else + maxMonthDay = 28; + } + if (Day > maxMonthDay) + return false; + + if (Hour > 23) + return false; + + if (Minute > 59) + return false; + + if (Second > 60) + return false; + + if (MicroSecond > 999999) + return false; + + if (Year == 1970 && Month == 1 && Day == 1) { + if ((i64)(3600 * Hour + 60 * Minute + Second) < (60 * ZoneOffsetMinutes)) + return false; + } + + return true; + } + + TInstant ToInstant(TInstant defaultValue) const { + time_t tt = ToTimeT(-1); + if (tt == -1) + return defaultValue; + return TInstant::Seconds(tt) + TDuration::MicroSeconds(MicroSecond); + } + + time_t ToTimeT(time_t defaultValue) const { + if (!IsOk()) + return defaultValue; + struct tm tm; + Zero(tm); + tm.tm_year = Year - 1900; + tm.tm_mon = Month - 1; + tm.tm_mday = Day; + tm.tm_hour = Hour; + tm.tm_min = Minute; + tm.tm_sec = Second; + time_t tt = TimeGM(&tm); + if (tt == -1) + return defaultValue; + return tt - ZoneOffsetMinutes * 60; + } +}; + +class TDateTimeParserBaseDeprecated { +public: + const TDateTimeFieldsDeprecated& GetDateTimeFields() const { + return DateTimeFields; + } + +protected: + TDateTimeFieldsDeprecated DateTimeFields; + int cs; //for ragel + int Sign; + int I; + int Dc; + +protected: + TDateTimeParserBaseDeprecated() + : DateTimeFields() + , cs(0) + , Sign(0) + , I(0xDEADBEEF) // to guarantee unittest break if ragel code is incorrect + , Dc(0xDEADBEEF) + { + } + + inline TInstant GetResult(int firstFinalState, TInstant defaultValue) const { + if (cs < firstFinalState) + return defaultValue; + return DateTimeFields.ToInstant(defaultValue); + } +}; + #define DECLARE_PARSER(CLASS) \ struct CLASS: public TDateTimeParserBaseDeprecated { \ CLASS(); \ bool ParsePart(const char* input, size_t len); \ TInstant GetResult(TInstant defaultValue) const; \ - }; - -DECLARE_PARSER(TIso8601DateTimeParserDeprecated) -DECLARE_PARSER(TRfc822DateTimeParserDeprecated) -DECLARE_PARSER(THttpDateTimeParserDeprecated) -DECLARE_PARSER(TX509ValidityDateTimeParserDeprecated) -DECLARE_PARSER(TX509Validity4yDateTimeParserDeprecated) - -#undef DECLARE_PARSER + }; + +DECLARE_PARSER(TIso8601DateTimeParserDeprecated) +DECLARE_PARSER(TRfc822DateTimeParserDeprecated) +DECLARE_PARSER(THttpDateTimeParserDeprecated) +DECLARE_PARSER(TX509ValidityDateTimeParserDeprecated) +DECLARE_PARSER(TX509Validity4yDateTimeParserDeprecated) + +#undef DECLARE_PARSER |