diff --git a/package-lock.json b/package-lock.json index 404a828..01d0b6d 100644 Binary files a/package-lock.json and b/package-lock.json differ diff --git a/package.json b/package.json index e59b826..dd2fc0a 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,11 @@ "dev:server": "grunt dev:server", "deploy": "grunt deploy" }, + "dependencies": { + "luxon": "^2.3.1" + }, "devDependencies": { + "@types/luxon": "^2.3.0", "grunt": "^1.4.1", "grunt-cli": "^1.4.3", "grunt-contrib-clean": "^2.0.0", diff --git a/src/main/js/Main.ts b/src/main/js/Main.ts index 52cf505..0413f1d 100644 --- a/src/main/js/Main.ts +++ b/src/main/js/Main.ts @@ -1,6 +1,8 @@ // @ts-ignore const {$, doAfterLoad, footer, header, nav} = window.fwdekker; +import {DateTime} from "luxon"; + /** * Returns a number between `min` (inclusive) and `max` (inclusive). @@ -255,7 +257,7 @@ class DoomsdayDate { /** * The underlying date. */ - readonly date: Date; + readonly date: DateTime; /** @@ -263,7 +265,7 @@ class DoomsdayDate { * * @param date the date to be wrapped */ - constructor(date: Date) { + constructor(date: DateTime) { this.date = date; } @@ -274,7 +276,7 @@ class DoomsdayDate { * @return the number of this `DoomsdayDate`'s century */ getCentury(): number { - return Math.floor(this.date.getFullYear() / 100); + return Math.floor(this.date.year / 100); } /** @@ -283,8 +285,7 @@ class DoomsdayDate { * @return the day of the week of the anchor of this `DoomsdayDate`'s century */ getCenturyAnchorString(): string { - const centuryAnchorNumber = (5 * (this.getCentury() % 4)) % 7 + 2; - return DoomsdayDate.getWeekDayOf(centuryAnchorNumber); + return this.date.set({year: this.getCentury() * 100, month: 4, day: 4}).setLocale("en-US").weekdayLong; }; /** @@ -293,11 +294,7 @@ class DoomsdayDate { * @return the day of the week of the anchor day of this `DoomsdayDate`'s year */ getYearAnchorString(): string { - const anchorDate = new Date(this.date); - anchorDate.setDate(4); // 4th - anchorDate.setMonth(3); // April - anchorDate.setHours(0); // midnight local time - return DoomsdayDate.getWeekDayOf(anchorDate); + return this.date.set({day: 4, month: 4}).setLocale("en-US").weekdayLong; }; /** @@ -306,41 +303,10 @@ class DoomsdayDate { * @return the day of the week of this `DoomsdayDate` */ getWeekdayString(): string { - return DoomsdayDate.getWeekDayOf(this.date); + return this.date.setLocale("en-US").weekdayLong; }; - /** - * Returns the week day of [date]. - * - * @param date the date to get the week day of; if it is a `number`, then 0 corresponds to Sunday - * @return the name of the week day corresponding to [date] - */ - static getWeekDayOf(date: Date|number): string { - if (date instanceof Date) { - return date.toLocaleString("en-US", {weekday: "long"}); - } else { - switch (date % 7) { - case 0: - return "Sunday"; - case 1: - return "Monday"; - case 2: - return "Tuesday"; - case 3: - return "Wednesday"; - case 4: - return "Thursday"; - case 5: - return "Friday"; - case 6: - return "Saturday"; - default: - throw new Error(`Unexpected weekday number '${date}'.`); - } - } - }; - /** * Returns the day of the week corresponding to `dayString`, or an empty string if no day was recognized. * @@ -377,9 +343,9 @@ class DoomsdayDate { */ static random() { // TODO Give custom date range to this method - const minDate = new Date("0001-01-01").getTime() / 86400000; - const maxDate = new Date("9999-12-31").getTime() / 86400000; - return new DoomsdayDate(new Date(generateRandom(minDate, maxDate) * 86400000)); + const startDate = DateTime.utc(1, 1, 1); + const dayRange = 9999 * 365 + (10000 / 400 * 97) - 1; + return new DoomsdayDate(startDate.plus({days: generateRandom(0, dayRange)})) } } @@ -467,7 +433,7 @@ doAfterLoad(() => { updateTitle() { if (yearDetails.isOpened()) - this.titleLabel.innerText = `Doomsday of year ${quizDate.date.getFullYear()}?`; + this.titleLabel.innerText = `Doomsday of year ${quizDate.date.year}?`; else this.titleLabel.innerText = `Year`; } @@ -491,7 +457,7 @@ doAfterLoad(() => { } updateTitle() { - this.titleLabel.innerText = `Weekday of ${quizDate.date.toISOString().substring(0, 10)}?`; + this.titleLabel.innerText = `Weekday of ${quizDate.date.toISODate()}?`; } }($("#day-input"), $("#day-title-label"), $("#day-submit")); @@ -509,7 +475,7 @@ doAfterLoad(() => { function reloadQuiz() { quizDate = DoomsdayDate.random(); console.log("# Reset"); - console.log(`New date: ${quizDate.date.toISOString().substring(0, 10)}`); + console.log(`New date: ${quizDate.date.toISODate()}`); console.log(` ${quizDate.date}`); console.log(`Century#: ${quizDate.getCentury()}`); console.log(`Century: ${quizDate.getCenturyAnchorString()}`);