summaryrefslogtreecommitdiff
path: root/school/node_modules/pronote-api/src/data/dates.js
blob: 52377ba255a1dcb72b3fba9de2eba7f868f27869 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
function toPronoteWeek(session, date)
{
    const firstWeek = toUTCWeek(session.params.firstDay);
    const week = toUTCWeek(date);

    if (week >= firstWeek) {
        return week - firstWeek + 1;
    }

    return 52 - (firstWeek - week) + 1; // Trust me this works
}

function toUTCWeek(date)
{
    const firstDay = new Date((new Date()).getFullYear(), 0, 1);
    return Math.ceil((((date - firstDay) / 86400000) + firstDay.getDay() + 1) / 7);
}

function toPronoteDay(session, date)
{
    return Math.ceil((date - session.params.firstDay) / 86400000);
}

function fromPronoteDay(session, day)
{
    const date = new Date(session.params.firstDay.getTime());
    date.setDate(date.getDate() + day - 1);

    return date;
}

function toPronoteDate(date)
{
    return `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()} ` +
           `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
}

function fromPronoteHours(hours)
{
    return ~~hours[0] + ~~hours.substring(2) / 60;
}

function parseDate(string)
{
    const date = new Date();
    const split = string.split(' ');

    const day = split[0].split('/');

    date.setFullYear(~~day[2], (~~day[1]) - 1, ~~day[0]);
    date.setMilliseconds(0);

    if (split.length > 1)
    {
        const time = split[1].split(':');

        date.setHours(~~time[0]);
        date.setMinutes(~~time[1]);
        date.setSeconds(~~time[2]);
    }
    else
    {
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
    }

    return date;
}

module.exports = {
    toPronoteWeek,
    toUTCWeek,

    toPronoteDay,
    fromPronoteDay,

    toPronoteDate,
    parseDate,

    fromPronoteHours
};