summaryrefslogtreecommitdiff
path: root/jstests/libs/dateutil.js
diff options
context:
space:
mode:
authorAsya Kamsky <asya999@gmail.com>2017-06-08 11:53:12 -0400
committerCharlie Swanson <charlie.swanson@mongodb.com>2017-06-12 16:40:44 -0400
commit90c32254af51f11aa494bc061f0afe2669c1189d (patch)
treeeea8474d1ae969c70c6ec0117ec5ef585bdd609f /jstests/libs/dateutil.js
parent0e4f02db8931419e8c648852daedcb02e97291b8 (diff)
downloadmongo-90c32254af51f11aa494bc061f0afe2669c1189d.tar.gz
SERVER-9406 treat ObjectId type as Date in aggregation date expressions
Closes #1154 Signed-off-by: Charlie Swanson <charlie.swanson@mongodb.com>
Diffstat (limited to 'jstests/libs/dateutil.js')
-rw-r--r--jstests/libs/dateutil.js65
1 files changed, 65 insertions, 0 deletions
diff --git a/jstests/libs/dateutil.js b/jstests/libs/dateutil.js
new file mode 100644
index 00000000000..485e07020ee
--- /dev/null
+++ b/jstests/libs/dateutil.js
@@ -0,0 +1,65 @@
+"use strict";
+
+/**
+ * Helpers for generating test dates for aggregations
+ */
+var DateUtil = (function() {
+
+ /**
+ * local function to add leading 0 to month or day if needed.
+ */
+ function padded(val) {
+ return ("00" + val).slice(-2);
+ }
+
+ function getNewYear(year) {
+ return new Date("" + year + "-01-01T00:00:00Z");
+ }
+
+ function getEndOfFirstWeekInYear(year, day) {
+ return new Date("" + year + "-01-" + (padded(7 - day + 1)) + "T23:59:59Z");
+ }
+
+ function getStartOfSecondWeekInYear(year, day) {
+ return new Date("" + year + "-01-" + (padded(7 - day + 2)) + "T00:00:00Z");
+ }
+
+ function getBirthday(year) {
+ return new Date("" + year + "-07-05T21:36:00+02:00");
+ }
+
+ function getEndOfSecondToLastWeekInYear(year, day, type) {
+ if (type === 'leap') {
+ return new Date("" + year + "-12-" + padded(31 - day - 1) + "T23:59:59Z");
+ } else {
+ return new Date("" + year + "-12-" + padded(31 - day) + "T23:59:59Z");
+ }
+ }
+
+ function getStartOfLastWeekInYear(year, day, type) {
+ if (type === 'leap') {
+ return new Date("" + year + "-12-" + padded(31 - day) + "T00:00:00Z");
+ } else {
+ return new Date("" + year + "-12-" + padded(31 - day + 1) + "T00:00:00Z");
+ }
+ }
+
+ function getNewYearsEve(year) {
+ return new Date("" + year + "-12-31T23:59:59Z");
+ }
+
+ function shiftWeekday(dayOfWeek, daysToAdd) {
+ return ((dayOfWeek - 1 + daysToAdd) % 7) + 1;
+ }
+
+ return {
+ getNewYear: getNewYear,
+ getEndOfFirstWeekInYear: getEndOfFirstWeekInYear,
+ getStartOfSecondWeekInYear: getStartOfSecondWeekInYear,
+ getBirthday: getBirthday,
+ getEndOfSecondToLastWeekInYear: getEndOfSecondToLastWeekInYear,
+ getStartOfLastWeekInYear: getStartOfLastWeekInYear,
+ getNewYearsEve: getNewYearsEve,
+ shiftWeekday: shiftWeekday
+ };
+})();