summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/datetime
diff options
context:
space:
mode:
authorDavid Percy <david.percy@mongodb.com>2021-02-16 23:00:38 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-03-04 22:11:37 +0000
commit8641dd510c5d2b6fe8f58f221ff4fe724f9267a7 (patch)
tree92edc79d46c313b55aec48a1c7b5b20c87a6fed3 /src/mongo/db/query/datetime
parentfccad833c9081bf0cd61364afdb4ec01ceeb42fa (diff)
downloadmongo-8641dd510c5d2b6fe8f58f221ff4fe724f9267a7.tar.gz
SERVER-54233 Implement $derivative window-function executor
Diffstat (limited to 'src/mongo/db/query/datetime')
-rw-r--r--src/mongo/db/query/datetime/date_time_support.cpp32
-rw-r--r--src/mongo/db/query/datetime/date_time_support.h13
2 files changed, 45 insertions, 0 deletions
diff --git a/src/mongo/db/query/datetime/date_time_support.cpp b/src/mongo/db/query/datetime/date_time_support.cpp
index 096504a9e46..80778737520 100644
--- a/src/mongo/db/query/datetime/date_time_support.cpp
+++ b/src/mongo/db/query/datetime/date_time_support.cpp
@@ -590,6 +590,7 @@ auto const kDaysInNonLeapYear = 365LL;
auto const kHoursPerDay = 24LL;
auto const kMinutesPerHour = 60LL;
auto const kSecondsPerMinute = 60LL;
+auto const kMillisecondsPerSecond = 1000LL;
auto const kDaysPerWeek = 7;
auto const kQuartersPerYear = 4LL;
auto const kQuarterLengthInMonths = 3LL;
@@ -977,4 +978,35 @@ Date_t dateAdd(Date_t date, TimeUnit unit, long long amount, const TimeZone& tim
timelib_time_dtor(newTime);
return returnDate;
}
+
+StatusWith<long long> timeUnitTypicalMilliseconds(TimeUnit unit) {
+ auto constexpr millisecond = 1;
+ auto constexpr second = millisecond * kMillisecondsPerSecond;
+ auto constexpr minute = second * kSecondsPerMinute;
+ auto constexpr hour = minute * kMinutesPerHour;
+ auto constexpr day = hour * kHoursPerDay;
+ auto constexpr week = day * kDaysPerWeek;
+
+ switch (unit) {
+ case TimeUnit::millisecond:
+ return millisecond;
+ case TimeUnit::second:
+ return second;
+ case TimeUnit::minute:
+ return minute;
+ case TimeUnit::hour:
+ return hour;
+ case TimeUnit::day:
+ return day;
+ case TimeUnit::week:
+ return week;
+ case TimeUnit::month:
+ case TimeUnit::quarter:
+ case TimeUnit::year:
+ return Status(ErrorCodes::BadValue,
+ str::stream() << "TimeUnit is too big: " << serializeTimeUnit(unit));
+ }
+ MONGO_UNREACHABLE_TASSERT(5423303);
+}
+
} // namespace mongo
diff --git a/src/mongo/db/query/datetime/date_time_support.h b/src/mongo/db/query/datetime/date_time_support.h
index b4c1fca1007..7012ca2dc06 100644
--- a/src/mongo/db/query/datetime/date_time_support.h
+++ b/src/mongo/db/query/datetime/date_time_support.h
@@ -599,4 +599,17 @@ long long dateDiff(Date_t startDate,
* timezone - the timezone in which the start date is interpreted
*/
Date_t dateAdd(Date_t date, TimeUnit unit, long long amount, const TimeZone& timezone);
+
+/**
+ * Convert (approximately) a TimeUnit to a number of milliseconds.
+ *
+ * The answer is approximate because TimeUnit represents an amount of calendar time:
+ * for example, some calendar days are 23 or 25 hours long due to daylight savings time.
+ * This function assumes everything is "typical": days are 24 hours, minutes are 60 seconds.
+ *
+ * Large time units, 'month' or longer, are so variable that we don't try to pick a value: we
+ * return a non-OK Status.
+ */
+StatusWith<long long> timeUnitTypicalMilliseconds(TimeUnit unit);
+
} // namespace mongo