summaryrefslogtreecommitdiff
path: root/jstests/libs/dateutil.js
blob: 485e07020ee64ea715b8c7b9418846210dbe252c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
    };
})();