Merge pull request #42049 from ucasfl/parse-date

This commit is contained in:
Vladimir C 2022-10-06 11:11:42 +02:00 committed by GitHub
commit 0513824fa2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 0 deletions

View File

@ -116,6 +116,8 @@ ReturnType parseDateTimeBestEffortImpl(
bool is_am = false;
bool is_pm = false;
bool has_comma_between_date_and_time = false;
auto read_alpha_month = [&month] (const auto & alpha)
{
if (0 == strncasecmp(alpha, "Jan", 3)) month = 1;
@ -137,6 +139,15 @@ ReturnType parseDateTimeBestEffortImpl(
while (!in.eof())
{
if ((year && !has_time) || (!year && has_time))
{
if (*in.position() == ',')
{
has_comma_between_date_and_time = true;
++in.position();
}
}
char digits[std::numeric_limits<UInt64>::digits10];
size_t num_digits = 0;
@ -552,6 +563,10 @@ ReturnType parseDateTimeBestEffortImpl(
}
}
//// Date like '2022/03/04, ' should parse fail?
if (has_comma_between_date_and_time && (!has_time || !year || !month || !day_of_month))
return on_error("Cannot read DateTime: unexpected word after Date", ErrorCodes::CANNOT_PARSE_DATETIME);
/// If neither Date nor Time is parsed successfully, it should fail
if (!year && !month && !day_of_month && !has_time)
return on_error("Cannot read DateTime: neither Date nor Time was parsed successfully", ErrorCodes::CANNOT_PARSE_DATETIME);

View File

@ -0,0 +1,10 @@
2017-12-01 18:31:44
2017-01-12 18:31:44
2017-12-01 18:31:44
2017-01-12 18:31:44
2017-12-01 18:31:44
2017-01-12 18:31:44
2015-12-31 18:31:44
2015-12-31 18:31:44
2015-12-31 18:31:44
2015-12-31 18:31:44

View File

@ -0,0 +1,16 @@
select parseDateTimeBestEffort('01/12/2017, 18:31:44');
select parseDateTimeBestEffortUS('01/12/2017, 18:31:44');
select parseDateTimeBestEffort('01/12/2017,18:31:44');
select parseDateTimeBestEffortUS('01/12/2017,18:31:44');
select parseDateTimeBestEffort('01/12/2017 , 18:31:44');
select parseDateTimeBestEffortUS('01/12/2017 ,18:31:44');
select parseDateTimeBestEffortUS('18:31:44, 31/12/2015');
select parseDateTimeBestEffortUS('18:31:44 , 31/12/2015');
select parseDateTimeBestEffort('18:31:44, 31/12/2015');
select parseDateTimeBestEffort('18:31:44 , 31/12/2015');
select parseDateTimeBestEffort('01/12/2017,'); -- { serverError CANNOT_PARSE_DATETIME }
select parseDateTimeBestEffortUS('18:31:44,,,, 31/12/2015'); -- { serverError CANNOT_PARSE_DATETIME }
select parseDateTimeBestEffortUS('18:31:44, 31/12/2015,'); -- { serverError CANNOT_PARSE_TEXT }
select parseDateTimeBestEffort('01/12/2017, 18:31:44,'); -- { serverError CANNOT_PARSE_TEXT }
select parseDateTimeBestEffort('01/12/2017, ,,,18:31:44'); -- { serverError CANNOT_PARSE_DATETIME }
select parseDateTimeBestEffort('18:31:44 ,,,,, 31/12/2015'); -- { serverError CANNOT_PARSE_DATETIME }