PineaFan | e69bd22 | 2022-12-04 22:18:20 +0000 | [diff] [blame] | 1 | function getSeason() { |
| 2 | let year = new Date().getFullYear(); |
| 3 | |
| 4 | const dates = { |
| 5 | pride: [new Date(`${year}-6-1`), new Date(`${year}-6-31`)], |
| 6 | halloween: [new Date(`${year}-10-25`), new Date(`${year}-11-1`)], |
| 7 | trans: [new Date(`${year}-11-13`), new Date(`${year}-11-19`)], |
PineappleFan | 37476d2 | 2023-02-03 21:36:40 +0000 | [diff] [blame] | 8 | christmas: [new Date(`${year}-12-1`), new Date(`${year}-12-26`)], |
| 9 | aprilFools: [new Date(`${year}-04-01`), new Date(`${year}-04-01`)] |
PineaFan | e69bd22 | 2022-12-04 22:18:20 +0000 | [diff] [blame] | 10 | } |
| 11 | const filePaths = { |
| 12 | normal: "normal", |
| 13 | halloween: "seasonal/halloween", |
| 14 | christmas: "seasonal/christmas", |
| 15 | pride: "seasonal/pride", |
PineappleFan | 37476d2 | 2023-02-03 21:36:40 +0000 | [diff] [blame] | 16 | trans: "seasonal/trans", |
| 17 | aprilFools: "seasonal/aprilFools" |
PineaFan | e69bd22 | 2022-12-04 22:18:20 +0000 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | let current = new Date(); |
| 21 | let currentSeason = Object.keys(dates).find((str) => current >= dates[str][0] && current <= dates[str][1]) || "normal"; |
Ash | efe6f6b | 2023-02-13 22:07:43 +0000 | [diff] [blame^] | 22 | let daysIntoSeason; |
PineappleFan | e9ce4cc | 2023-02-13 22:05:07 +0000 | [diff] [blame] | 23 | if (currentSeason !== "normal") { |
| 24 | daysIntoSeason = Math.floor((new Date().getTime() - dates[currentSeason][0].getTime()) / (1000 * 60 * 60 * 24)) |
Ash | efe6f6b | 2023-02-13 22:07:43 +0000 | [diff] [blame^] | 25 | } else { |
| 26 | // Calculate days past the most recent season |
| 27 | let mostRecentSeason = Object.keys(dates).reduce((a, b) => dates[a][1] > dates[b][1] ? a : b); |
| 28 | daysIntoSeason = Math.floor((new Date().getTime() - dates[mostRecentSeason][1].getTime()) / (1000 * 60 * 60 * 24)) |
PineappleFan | e9ce4cc | 2023-02-13 22:05:07 +0000 | [diff] [blame] | 29 | } |
PineaFan | e69bd22 | 2022-12-04 22:18:20 +0000 | [diff] [blame] | 30 | |
| 31 | return { |
| 32 | season: currentSeason, |
PineappleFan | e9ce4cc | 2023-02-13 22:05:07 +0000 | [diff] [blame] | 33 | filePath: filePaths[currentSeason], |
| 34 | daysIntoSeason: daysIntoSeason |
PineaFan | e69bd22 | 2022-12-04 22:18:20 +0000 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
| 38 | const Season = async (req, res) => { |
| 39 | return res.status(200).send(getSeason()); |
| 40 | } |
| 41 | |
| 42 | export default Season; |
| 43 | export { getSeason }; |