From d7b3b23fdfea0b445c9ef3c3c44c6a1dcc579183 Mon Sep 17 00:00:00 2001 From: jackwener <30525741+jackwener@users.noreply.github.com> Date: Mon, 24 Jan 2022 19:56:24 +0800 Subject: [PATCH 1/5] parse date --- src/common/time/parser/datetime_parser.yy | 8 ++++++++ src/common/time/parser/test/DateTimeParserTest.cpp | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/common/time/parser/datetime_parser.yy b/src/common/time/parser/datetime_parser.yy index f6d04d957e9..9a654a7ecec 100644 --- a/src/common/time/parser/datetime_parser.yy +++ b/src/common/time/parser/datetime_parser.yy @@ -131,6 +131,14 @@ date throw DatetimeParser::syntax_error(@1, result.toString()); } } + | INTEGER { + $$ = new nebula::Date($1, 1, 1); + auto result = nebula::time::TimeUtils::validateDate(*$$); + if (!result.ok()) { + delete $$; + throw DatetimeParser::syntax_error(@1, result.toString()); + } + } ; time diff --git a/src/common/time/parser/test/DateTimeParserTest.cpp b/src/common/time/parser/test/DateTimeParserTest.cpp index 939441b48b4..56b4c07676b 100644 --- a/src/common/time/parser/test/DateTimeParserTest.cpp +++ b/src/common/time/parser/test/DateTimeParserTest.cpp @@ -137,6 +137,13 @@ TEST(DatetimeReader, Date) { ASSERT_TRUE(result.ok()) << result.status(); EXPECT_EQ(nebula::Date(2019, 1, 1), result.value()); } + // lack month and day + { + auto parser = time::DatetimeReader::makeDateReader(); + auto result = parser.readDate("2019"); + ASSERT_TRUE(result.ok()) << result.status(); + EXPECT_EQ(nebula::Date(2019, 1, 1), result.value()); + } // TODO // lack month // { @@ -182,6 +189,11 @@ TEST(DatetimeReader, DateFailed) { auto result = parser.readDate("2019-01-"); EXPECT_FALSE(result.ok()) << result.value(); } + { + auto parser = time::DatetimeReader::makeDateReader(); + auto result = parser.readDate("2019-"); + EXPECT_FALSE(result.ok()) << result.value(); + } // not exits prefix { auto parser = time::DatetimeReader::makeDateReader(); From 7be1ffcc563d12aa4ceec7c794303b277492f09c Mon Sep 17 00:00:00 2001 From: jackwener <30525741+jackwener@users.noreply.github.com> Date: Tue, 25 Jan 2022 15:08:58 +0800 Subject: [PATCH 2/5] enhance datetime --- src/common/time/parser/DatetimeReader.h | 7 ++ src/common/time/parser/datetime_parser.yy | 53 +++++++++----- src/common/time/parser/datetime_scanner.lex | 4 ++ .../time/parser/test/DateTimeParserTest.cpp | 70 +++++++++++++------ 4 files changed, 95 insertions(+), 39 deletions(-) diff --git a/src/common/time/parser/DatetimeReader.h b/src/common/time/parser/DatetimeReader.h index dfa8340f1f8..fda6f824301 100644 --- a/src/common/time/parser/DatetimeReader.h +++ b/src/common/time/parser/DatetimeReader.h @@ -33,16 +33,19 @@ class DatetimeReader { } StatusOr readDatetime(std::string input) { + input = kDatetimePrefix + input; return read(std::move(input)); } StatusOr readDate(std::string input) { + input = kDatePrefix + input; auto result = read(std::move(input)); NG_RETURN_IF_ERROR(result); return result.value().date(); } StatusOr