summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNaomie Gao <naomie.gao@mongodb.com>2022-06-21 19:33:33 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-06-21 20:41:16 +0000
commitd59afee3a6ed85e1614c2a030426ac3754c77ea1 (patch)
tree1a090212e3dccab62f750b9dc1471cfe059d5cbd
parent765e3c89b711b071d3bfa3ec9a0b5b4daf8a97c4 (diff)
downloadmongo-d59afee3a6ed85e1614c2a030426ac3754c77ea1.tar.gz
SERVER-32863 Add named month placeholder to $dateFromString format
-rw-r--r--dump_python3.15756.corebin0 -> 102465536 bytes
-rw-r--r--jstests/aggregation/expressions/date_from_string.js26
-rw-r--r--src/mongo/db/query/datetime/date_time_support.cpp3
3 files changed, 29 insertions, 0 deletions
diff --git a/dump_python3.15756.core b/dump_python3.15756.core
new file mode 100644
index 00000000000..c2e4a6c0961
--- /dev/null
+++ b/dump_python3.15756.core
Binary files differ
diff --git a/jstests/aggregation/expressions/date_from_string.js b/jstests/aggregation/expressions/date_from_string.js
index 9b4d0702e5e..a1257781b27 100644
--- a/jstests/aggregation/expressions/date_from_string.js
+++ b/jstests/aggregation/expressions/date_from_string.js
@@ -569,6 +569,27 @@ testCases.forEach(function(testCase) {
});
/* --------------------------------------------------------------------------------------- */
+/* Tests for textual month. */
+
+testCases = [
+ {inputString: "2017, July 4", format: "%Y, %B %d", expect: "2017-07-04T00:00:00Z"},
+ {inputString: "oct 20 2020", format: "%b %d %Y", expect: "2020-10-20T00:00:00Z"},
+];
+testCases.forEach(function(testCase) {
+ assert.eq(
+ [{_id: 0, date: ISODate(testCase.expect)}],
+ coll.aggregate({
+ $project: {
+ date: {
+ $dateFromString: {dateString: testCase.inputString, format: testCase.format}
+ }
+ }
+ })
+ .toArray(),
+ tojson(testCase));
+});
+
+/* --------------------------------------------------------------------------------------- */
/* Testing whether it throws the right assert for missing elements of a date/time string. */
coll.drop();
@@ -763,6 +784,11 @@ assertErrCodeAndErrMsgContains(coll,
ErrorCodes.ConversionFailure,
"Mixing of ISO dates with natural dates is not allowed");
+pipeline =
+ [{$project: {date: {$dateFromString: {dateString: "Dece 31 2018", format: "%b %d %Y"}}}}];
+assertErrCodeAndErrMsgContains(
+ coll, pipeline, ErrorCodes.ConversionFailure, "Error parsing date string");
+
// Test embedded null bytes in the 'dateString' and 'format' fields.
pipeline =
[{$project: {date: {$dateFromString: {dateString: "12/31\0/2018", format: "%m/%d/%Y"}}}}];
diff --git a/src/mongo/db/query/datetime/date_time_support.cpp b/src/mongo/db/query/datetime/date_time_support.cpp
index 09badabd4a0..439c1f028d2 100644
--- a/src/mongo/db/query/datetime/date_time_support.cpp
+++ b/src/mongo/db/query/datetime/date_time_support.cpp
@@ -76,6 +76,8 @@ long long seconds(Date_t date) {
// Format specifier map when parsing a date from a string with a required format.
//
const std::vector<timelib_format_specifier> kDateFromStringFormatMap = {
+ {'b', TIMELIB_FORMAT_TEXTUAL_MONTH_3_LETTER},
+ {'B', TIMELIB_FORMAT_TEXTUAL_MONTH_FULL},
{'d', TIMELIB_FORMAT_DAY_TWO_DIGIT},
{'G', TIMELIB_FORMAT_YEAR_ISO},
{'H', TIMELIB_FORMAT_HOUR_TWO_DIGIT_24_MAX},
@@ -775,6 +777,7 @@ static const StringMap<DayOfWeek> dayOfWeekNameToDayOfWeekMap{
{"sunday", DayOfWeek::sunday},
{"sun", DayOfWeek::sunday},
};
+
} // namespace
long long dateDiff(Date_t startDate,