diff options
author | David Percy <david.percy@mongodb.com> | 2021-01-06 19:51:09 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-02-04 10:12:54 +0000 |
commit | c7c16d3c6fdcb24bde8a014b0ca2c0d4650ce9d2 (patch) | |
tree | 71f67314e2c557364bcea3a93b229e7e2abcd7cb /src/mongo/db/query/datetime | |
parent | 6eea989f7996dea108f937990d62cd55d1f14c8c (diff) | |
download | mongo-c7c16d3c6fdcb24bde8a014b0ca2c0d4650ce9d2.tar.gz |
SERVER-53399 Parse individual window functions
Diffstat (limited to 'src/mongo/db/query/datetime')
-rw-r--r-- | src/mongo/db/query/datetime/date_time_support.cpp | 36 | ||||
-rw-r--r-- | src/mongo/db/query/datetime/date_time_support.h | 9 |
2 files changed, 36 insertions, 9 deletions
diff --git a/src/mongo/db/query/datetime/date_time_support.cpp b/src/mongo/db/query/datetime/date_time_support.cpp index 425a4349abc..ea2f603b8bf 100644 --- a/src/mongo/db/query/datetime/date_time_support.cpp +++ b/src/mongo/db/query/datetime/date_time_support.cpp @@ -722,18 +722,40 @@ long long dateDiff(Date_t startDate, Date_t endDate, TimeUnit unit, const TimeZo } } -TimeUnit parseTimeUnit(const std::string_view unitName) { - const StringData unitNameAsStringData{unitName.data(), unitName.length()}; - auto iterator = timeUnitNameToTimeUnitMap.find(unitNameAsStringData); +TimeUnit parseTimeUnit(StringData unitName) { + auto iterator = timeUnitNameToTimeUnitMap.find(unitName); uassert(ErrorCodes::FailedToParse, - str::stream() << "unknown time unit value: " << unitNameAsStringData, + str::stream() << "unknown time unit value: " << unitName, iterator != timeUnitNameToTimeUnitMap.end()); return iterator->second; } -bool isValidTimeUnit(const std::string_view unitName) { - return timeUnitNameToTimeUnitMap.find(StringData{unitName.data(), unitName.length()}) != - timeUnitNameToTimeUnitMap.end(); +bool isValidTimeUnit(StringData unitName) { + return timeUnitNameToTimeUnitMap.find(unitName) != timeUnitNameToTimeUnitMap.end(); +} + +StringData serializeTimeUnit(TimeUnit unit) { + switch (unit) { + case TimeUnit::year: + return "year"_sd; + case TimeUnit::quarter: + return "quarter"_sd; + case TimeUnit::month: + return "month"_sd; + case TimeUnit::week: + return "week"_sd; + case TimeUnit::day: + return "day"_sd; + case TimeUnit::hour: + return "hour"_sd; + case TimeUnit::minute: + return "minute"_sd; + case TimeUnit::second: + return "second"_sd; + case TimeUnit::millisecond: + return "millisecond"_sd; + } + MONGO_UNREACHABLE_TASSERT(5339900); } void TimelibRelTimeDeleter::operator()(timelib_rel_time* relTime) { diff --git a/src/mongo/db/query/datetime/date_time_support.h b/src/mongo/db/query/datetime/date_time_support.h index 640ba01c5a6..43bc84d54fb 100644 --- a/src/mongo/db/query/datetime/date_time_support.h +++ b/src/mongo/db/query/datetime/date_time_support.h @@ -496,14 +496,19 @@ private: * TimeUnit. Throws an exception with error code ErrorCodes::FailedToParse when passed an invalid * name. */ -TimeUnit parseTimeUnit(const std::string_view unitName); +TimeUnit parseTimeUnit(StringData unitName); /** * Returns true if 'unitName' is a valid time unit, meaning that it can be parsed by the * 'parseTimeUnit()' function into one of the units represented by the 'TimeUnit' enum. Otherwise * returns 'false'. */ -bool isValidTimeUnit(const std::string_view unitName); +bool isValidTimeUnit(StringData unitName); + +/** + * Inverse of parseTimeUnit. + */ +StringData serializeTimeUnit(TimeUnit unit); /** * A custom-deleter which destructs a timelib_rel_time* when it goes out of scope. |