-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_date.js
90 lines (78 loc) · 2.3 KB
/
calc_date.js
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
83
84
85
86
87
88
89
90
const moment = require("moment");
const fs = require("node:fs");
const scrape = require("./lasveckor_scraper.js");
let dateDict = {};
// check if data.json exists
if (!fs.existsSync("./data/data.json")) {
scrape().then((res) => {
dateDict = res;
});
} else {
dateDict = JSON.parse(fs.readFileSync("./data/data.json"));
// check if it was updated after 1/7 this year and if 1/7 has occured this year
const updated = moment(dateDict.updated);
const firstOfJuly = moment().month(6).date(1);
if (updated.isBefore(firstOfJuly) && moment().isAfter(firstOfJuly)) {
scrape().then((res) => {
dateDict = res;
});
}
}
function readDatePeriod(currDate) {
let soughtDate = "";
let diff = -1000;
for (const dat in dateDict) {
// if dat is updated, easter_start, easter_end or ord_cont then skip
if (
dat === "updated" ||
dat === "easter_start" ||
dat === "easter_end" ||
dat === "ord_cont"
) {
continue;
}
const deltaT = moment(dat).diff(currDate, "days");
if (deltaT === 0) {
return { date: dat, type: dateDict[dat] };
}
if (deltaT > diff && deltaT < 0) {
soughtDate = dat;
diff = deltaT;
}
}
return { date: soughtDate, type: dateDict[soughtDate] };
}
function computeTime() {
// Find date where value is easter_start, easter_end and ord_cont in json file
const EASTER_START = dateDict.easter_start;
const ORD_CONT = dateDict.ord_cont;
const currentDate = moment();
const easterEndCheck = currentDate.diff(ORD_CONT, "days");
const easterStartCheck = currentDate.diff(EASTER_START, "days");
const { date: dat, type: typ } = readDatePeriod(currentDate);
if (typ === "exam_period") {
const deltaT = currentDate.diff(moment(dat), "days");
if (deltaT > 7) {
return "Självstudier";
}
return "Tentavecka";
}
let deltaT = currentDate.diff(moment(dat), "days");
if (typ === 'first_day') {
const weeks = Math.floor(deltaT / 7);
return `MV ${weeks + 1}`;
}
if (easterStartCheck >= 0 && easterEndCheck <= 0) {
return "Självstudier";
}
if (easterEndCheck > 0) {
// the easter period is over and it is 7 days long
deltaT = deltaT - 7;
}
const weeks = Math.floor(deltaT / 7);
if (weeks > 7) {
return "Självstudier";
}
return `LV ${weeks + 1}`;
}
module.exports = computeTime;