summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/datetime
diff options
context:
space:
mode:
authorJustin Seyster <justin.seyster@mongodb.com>2019-06-12 15:30:30 -0400
committerJustin Seyster <justin.seyster@mongodb.com>2019-07-01 14:24:13 -0400
commitd4843fc49931c7ce4332dc373623267c293eb518 (patch)
tree8c6dbf8f05ba37e30b12178e2c956f0d4f7f4b89 /src/mongo/db/query/datetime
parentfbf80b67edd2c7e46adb9090a31a298af8fc0f78 (diff)
downloadmongo-d4843fc49931c7ce4332dc373623267c293eb518.tar.gz
SERVER-40383 Handle week, day edge cases in timelib_date_from_isodate
Diffstat (limited to 'src/mongo/db/query/datetime')
-rw-r--r--src/mongo/db/query/datetime/date_time_support_test.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/mongo/db/query/datetime/date_time_support_test.cpp b/src/mongo/db/query/datetime/date_time_support_test.cpp
index a56bf6e01a2..f11973aa145 100644
--- a/src/mongo/db/query/datetime/date_time_support_test.cpp
+++ b/src/mongo/db/query/datetime/date_time_support_test.cpp
@@ -1099,5 +1099,61 @@ TEST(DateFromString, EmptyFormatStringThrowsForAllInputs) {
ErrorCodes::ConversionFailure);
}
+TEST(DayOfWeek, WeekNumber) {
+ long long year = 2019;
+ long long week = 1;
+ long long day = 1;
+ long long hour = 0;
+ long long minute = 0;
+ long long second = 0;
+ long long millisecond = 0;
+
+ Date_t baseline = kDefaultTimeZone.createFromIso8601DateParts(
+ year, week, day, hour, minute, second, millisecond);
+
+ long long weekDurationInMillis = 7 * 24 * 60 * 60 * 1000;
+
+ for (int weekIt = -10000; weekIt < 10000; weekIt++) {
+ // Calculate a date using the ISO 8601 week-numbered year method.
+ Date_t dateFromIso8601 = kDefaultTimeZone.createFromIso8601DateParts(
+ year, weekIt, day, hour, minute, second, millisecond);
+
+ // Calculate the same date by adding 'weekDurationInMillis' to 'baseline' for each week past
+ // the baseline date.
+ Date_t dateFromArithmetic = baseline + Milliseconds(weekDurationInMillis * (weekIt - 1));
+
+ // The two methods should produce the same time.
+ ASSERT_EQ(dateFromIso8601, dateFromArithmetic);
+ }
+}
+
+TEST(DayOfWeek, DayNumber) {
+ long long year = 2019;
+ long long week = 34;
+ long long day = 1;
+ long long hour = 0;
+ long long minute = 0;
+ long long second = 0;
+ long long millisecond = 0;
+
+ Date_t baseline = kDefaultTimeZone.createFromIso8601DateParts(
+ year, week, day, hour, minute, second, millisecond);
+
+ long long dayDurationInMillis = 24 * 60 * 60 * 1000;
+
+ for (int dayIt = -10000; dayIt < 10000; dayIt++) {
+ // Calculate a date using the ISO 8601 week-numbered year method.
+ Date_t dateFromIso8601 = kDefaultTimeZone.createFromIso8601DateParts(
+ year, week, dayIt, hour, minute, second, millisecond);
+
+ // Calculate the same date by adding 'dayDurationInMillis' to 'baseline' for each day past
+ // the baseline date.
+ Date_t dateFromArithmetic = baseline + Milliseconds(dayDurationInMillis * (dayIt - 1));
+
+ // The two methods should produce the same time.
+ ASSERT_EQ(dateFromIso8601, dateFromArithmetic);
+ }
+}
+
} // namespace
} // namespace mongo