-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-counter.js
202 lines (186 loc) · 5.26 KB
/
day-counter.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// This might be bad as it's written by Claude :)
const config = {
"totalWorkDays": 187,
"autumnSemester": {
"start": "2024-08-07",
"end": "2024-12-20",
"breaks": [
{
"name": "Syysloma (vko 42)",
"start": "2024-10-14",
"end": "2024-10-20"
},
{
"name": "Itsenäisyyspäivä",
"date": "2024-12-06"
}
]
},
"christmasBreak": {
"start": "2024-12-21",
"end": "2025-01-06"
},
"springSemester": {
"start": "2025-01-07",
"end": "2025-05-31",
"breaks": [
{
"name": "Talviloma (vko 9)",
"start": "2025-02-24",
"end": "2025-03-02"
},
{
"name": "Pääsiäinen",
"start": "2025-04-18",
"end": "2025-04-21"
},
{
"name": "Vappuvapaa",
"start": "2025-05-01",
"end": "2025-05-02"
},
{
"name": "Helatorstai",
"date": "2025-05-29"
}
]
},
"abiturientsFinalDay": "2025-02-06",
"periods": [
{
"number": 1,
"start": "2024-08-07",
"end": "2024-09-26",
"days": 37,
"finalWeek": {
"start": "2024-09-19",
"end": "2024-09-26"
}
},
{
"number": 2,
"start": "2024-09-27",
"end": "2024-11-21",
"days": 35,
"finalWeek": {
"start": "2024-11-14",
"end": "2024-11-21"
}
},
{
"number": 3,
"start": "2024-11-22",
"end": "2025-01-30",
"days": 38,
"finalWeek": {
"start": "2025-01-23",
"end": "2025-01-30"
}
},
{
"number": 4,
"start": "2025-01-31",
"end": "2025-03-31",
"days": 37,
"finalWeek": {
"start": "2025-03-24",
"end": "2025-03-31"
}
},
{
"number": 5,
"start": "2025-04-01",
"end": "2025-05-31",
"days": 39,
"finalWeek": {
"start": "2025-05-21",
"end": "2025-05-28"
}
}
]
}
function getDaysBetween(date1, date2) {
const oneDay = 24 * 60 * 60 * 1000;
return Math.max(0, Math.round((date2 - date1) / oneDay));
}
function getNextBreak(currentDate) {
const breaks = [
...config.autumnSemester.breaks,
{name: "Joululoma", start: config.christmasBreak.start, end: config.christmasBreak.end},
...config.springSemester.breaks
];
let ongoing = false;
for (let breakPeriod of breaks) {
const breakStart = new Date(breakPeriod.start || breakPeriod.date);
const breakEnd = new Date(breakPeriod.end || breakPeriod.date);
breakEnd.setHours(23, 59, 59);
if (breakStart > currentDate) {
return { ...breakPeriod, ongoing };
} else if (breakEnd > currentDate) {
ongoing = true;
}
}
return null;
}
function getNextPeriodStart(currentDate) {
for (let period of config.periods) {
const periodStart = new Date(period.start);
if (periodStart > currentDate) {
return periodStart;
}
}
return null;
}
function getCurrentPeriod(currentDate) {
for (let i = config.periods.length - 1; i >= 0; i--) {
const periodStart = new Date(config.periods[i].start);
if (currentDate >= periodStart) {
return config.periods[i];
}
}
return null;
}
function generateOutput(currentDate) {
const nextBreak = getNextBreak(currentDate);
const currentPeriod = getCurrentPeriod(currentDate);
const nextPeriodStart = getNextPeriodStart(currentDate);
const abiturientsFinalDay = new Date(config.abiturientsFinalDay);
let output = "<p>";
if (nextBreak) {
if (nextBreak.ongoing) {
output += "Mitä ihmettä sinä täällä lomalla teet? ";
}
const breakStart = new Date(nextBreak.start || nextBreak.date);
const daysUntilBreak = getDaysBetween(currentDate, breakStart);
const schoolDaysUntilBreak = Math.round(daysUntilBreak * 5 / 7); // Approximation
output += `Seuraava loma on ${nextBreak.name}, johon on ${daysUntilBreak} päivä${daysUntilBreak !== 1 ? 'ä' : ''} (~${schoolDaysUntilBreak} koulupäivä${schoolDaysUntilBreak !== 1 ? 'ä' : ''}). `;
} else {
output += "Nyt on lomat lusittu. ";
}
output += "</p><p>"
if (currentPeriod && currentPeriod !== config.periods[config.periods.length - 1]) {
const daysUntilFinalWeek = getDaysBetween(currentDate, new Date(currentPeriod.finalWeek.start));
if (daysUntilFinalWeek > 0) {
output += `Päättöviikon alkuun ${daysUntilFinalWeek} päivä${daysUntilFinalWeek !== 1 ? 'ä' : ''}`;
}
if (nextPeriodStart) {
const daysUntilNextPeriod = getDaysBetween(currentDate, nextPeriodStart);
if (daysUntilFinalWeek > 0) {
output += ` ja seuraavan periodin alkuun ${daysUntilNextPeriod} päivä${daysUntilNextPeriod !== 1 ? 'ä' : ''}. `;
} else {
output += `Seuraavan periodin alkuun ${daysUntilNextPeriod} päivä${daysUntilNextPeriod !== 1 ? 'ä' : ''}. `;
}
} else if (daysUntilFinalWeek > 0) {
output += '. ';
}
}
if (currentDate < abiturientsFinalDay) {
const daysUntilAbiturientsFinalDay = getDaysBetween(currentDate, abiturientsFinalDay);
output += `Abit lähtevät talosta ${daysUntilAbiturientsFinalDay} päivän kuluttua.`;
}
output += "</p>";
return output.trim();
}
// Usage
const today = new Date();
document.getElementById('day-counter').innerHTML = generateOutput(today);