diff options
author | Minteck <contact@minteck.org> | 2023-01-10 14:54:04 +0100 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2023-01-10 14:54:04 +0100 |
commit | 99c1d9af689e5325f3cf535c4007b3aeb8325229 (patch) | |
tree | e663b3c2ebdbd67c818ac0c5147f0ce1d2463cda /school/node_modules/pronote-api/src/data/dates.js | |
parent | 9871b03912fc28ad38b4037ebf26a78aa937baba (diff) | |
download | pluralconnect-99c1d9af689e5325f3cf535c4007b3aeb8325229.tar.gz pluralconnect-99c1d9af689e5325f3cf535c4007b3aeb8325229.tar.bz2 pluralconnect-99c1d9af689e5325f3cf535c4007b3aeb8325229.zip |
Update - This is an automated commit
Diffstat (limited to 'school/node_modules/pronote-api/src/data/dates.js')
-rw-r--r-- | school/node_modules/pronote-api/src/data/dates.js | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/school/node_modules/pronote-api/src/data/dates.js b/school/node_modules/pronote-api/src/data/dates.js new file mode 100644 index 0000000..52377ba --- /dev/null +++ b/school/node_modules/pronote-api/src/data/dates.js @@ -0,0 +1,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 +}; |