aboutsummaryrefslogtreecommitdiff
path: root/src/lib/DateUtils.js
blob: cf3e4da2d9b18ea456a31f48c1f2b92a9d1285bd (plain) (blame)
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
66
/* LUT for date-number from month name. */
const MonthNumber = {
    "January": 1, "Feb": 2, "March": 3, "April": 4, "May": 5, "June": 6, "July": 7, "August": 8, "September": 9, "October": 10, "November": 11, "December": 12
}

/**
 * Padds a string (or number) with leading zeroes.
 * @param  {string}     number The string to be padded.
 * @param  {number} [2] n      The minimum width of the returned string.
 * @return {string}            Zero-padded string.
 */
function zpad(number, n=2) {
    let ret = number.toString();
    while (n - ret.length > 0) {
        ret = "0" + ret;
    }
    return ret
}

/**
 * Attempts to recognize date-string pattern and convert into ISO861.
 * @param  {string} dateStr A String representing the time, UTC will be assumed
 *                          unless it is valid ISO861 with a different timezone.
 * @return {string}         dateStr as ISO861. If no pattern was found (or it
 *                          already is valid ISO861. The string will be returned
 *                          with no changes.
 */
function toISO861UTC(dateStr) {
    // yyyy-mm-ddThh:mm:ssZ
    if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/.test(dateStr)) {
        //console.log(`yyyy-mm-ddThh:mm:ssZ <- ${dateStr}`);
        return dateStr;
    }

    // yyyy MMMM dd
    if (/^\d{4}\s[a-z,A-Z]+\s\d{2}$/.test(dateStr)) {
        //console.log(`yyyy MMM dd <- ${dateStr}`);
        let parts = dateStr.split(" ");
        dateStr = (`${parts[0]}-${zpad(MonthNumber[parts[1]])}-${parts[2]}T00:00:00Z`);
        return dateStr;
    }

    // yyyy mm dd hh:mm:ss
    if (/^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}/.test(dateStr)) {
        return dateStr.replace(" ", "T") + "Z";
    }

    // yyyy mm dd hh:mm:ss.msm
    if (/^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}.\d{4}/.test(dateStr)) {
        return dateStr.replace(" ", "T").split(".")[0] + "Z";
    }

    console.warn(dateStr);
    return dateStr;
}

/**
 * Returns a date object from a date-string.
 * @param {string} dateStr A String representing the time, UTC will be assumed
 *                         unless it is valid ISO861 with a different timezone.
 * @return {Date}          The date object representing the date-string.
 */
export function parseDateAsUTC(dateStr) {
    dateStr = toISO861UTC(dateStr);
    return new Date(dateStr);
}