-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetClosingTime.js
More file actions
83 lines (77 loc) · 2.48 KB
/
Copy pathgetClosingTime.js
File metadata and controls
83 lines (77 loc) · 2.48 KB
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
/**
* Converts a German date in the format "Weekday, Day. Month
* Year" to "Day. Month Year", translating the month into
* English.
*
* @param {string} date The date string to convert
* @returns {string | undefined} The converted date string or undefined if the
* date string was not in the expected format
*/
function convertDate(date) {
const germanDate = date.split(", ")[1];
return germanDate
?.replace("Januar", "January")
?.replace("Februar", "February")
?.replace("März", "March")
?.replace("Mai", "May")
?.replace("Juni", "June")
?.replace("Juli", "July")
?.replace("Oktober", "October")
?.replace("Dezember", "December");
}
/**
* Depending on the weekday, the canteens close at different
* times. For the closing times see also the
* {@link https://www.stw-saarland.de/gastro/ page of the Studentenwerk}.
*
* The closing time of the canteen is the closing time of
* the latest counter. For example, for the canteen at the
* Saarland University, this means that it is the closing
* time of the counter for Menü 1 and Menü 2.
*
* `dateString` is expected to be a string in the format
* "Weekday, Day. Month Year" in German.
*
* @param {"sb" | "htwcas" | "musiksb"} canteen The canteen
* @param {string} dateString The date string of the day in
* the specified format
* @returns {Date | undefined} The closing time for the
* `canteen` and the day or undefined if the `dateString`
* was not in the expected format
*/
export function getClosingTime(canteen, dateString) {
const convertedDateString = convertDate(dateString);
if (convertedDateString === undefined) {
return;
}
const convertedDate = new Date(convertedDateString);
let closingMinutes = getClosingMinutes(canteen, convertedDate.getDay());
return new Date(
convertedDate.getFullYear(),
convertedDate.getMonth(),
convertedDate.getDate(),
14,
closingMinutes,
);
}
/**
* The function does not check whether `canteen` is one of
* the specified strings.
*
* @param {"sb" | "htwcas" | "musiksb"} canteen The canteen
* @param {number} day The day of the week in the same
* enumeration as the `getDay` property of `Date`
* @returns {0 | 15 | 30} The minutes of the closing time
* for the `canteen` and the day
*/
function getClosingMinutes(canteen, day) {
switch (canteen) {
case "musiksb":
return 0;
case "htwcas":
return 15;
case "sb":
// The canteen closes 15 minutes earlier on Fridays.
return day === 5 ? 15 : 30;
}
}