Calculate week day from UTC date

Thanks to Scott Ventura for reporting this issue! I hope it solves it.
This commit is contained in:
Florine W. Dekker 2021-11-13 11:18:09 +01:00
parent ac74a2f582
commit 8ed98be4a2
Signed by untrusted user: FWDekker
GPG Key ID: D3DCFAA8A4560BE0
3 changed files with 35 additions and 30 deletions

BIN
package-lock.json generated

Binary file not shown.

View File

@ -1,6 +1,6 @@
{ {
"name": "doomsday", "name": "doomsday",
"version": "1.3.16", "version": "1.3.17",
"description": "Test your mastery of Conway's Doomsday rule.", "description": "Test your mastery of Conway's Doomsday rule.",
"author": "F.W. Dekker", "author": "F.W. Dekker",
"browser": "dist/bundle.js", "browser": "dist/bundle.js",
@ -16,15 +16,15 @@
"deploy": "grunt deploy" "deploy": "grunt deploy"
}, },
"devDependencies": { "devDependencies": {
"grunt": "^1.4.0", "grunt": "^1.4.1",
"grunt-cli": "^1.4.2", "grunt-cli": "^1.4.3",
"grunt-contrib-clean": "^2.0.0", "grunt-contrib-clean": "^2.0.0",
"grunt-contrib-copy": "^1.0.0", "grunt-contrib-copy": "^1.0.0",
"grunt-contrib-watch": "^1.1.0", "grunt-contrib-watch": "^1.1.0",
"grunt-focus": "^1.0.0", "grunt-focus": "^1.0.0",
"grunt-text-replace": "^0.4.0", "grunt-text-replace": "^0.4.0",
"grunt-webpack": "^4.0.3", "grunt-webpack": "^5.0.0",
"webpack": "^5.36.0", "webpack": "^5.64.0",
"webpack-cli": "^4.6.0" "webpack-cli": "^4.9.1"
} }
} }

View File

@ -227,7 +227,7 @@ class ToggleableSection {
/** /**
* A wrapper around the good ol' `Date` class that provides a bunch of useful Doomsday-specific methods. * A wrapper around the good ol' `Date` class that provides a bunch of useful Doomsday-specific methods.
* *
* @property {Date} the underlying date * @property {Date} date the underlying date
*/ */
class DoomsdayDate { class DoomsdayDate {
/** /**
@ -256,7 +256,7 @@ class DoomsdayDate {
*/ */
getCenturyAnchorString() { getCenturyAnchorString() {
const centuryAnchorNumber = (5 * (this.getCentury() % 4)) % 7 + 2; const centuryAnchorNumber = (5 * (this.getCentury() % 4)) % 7 + 2;
return DoomsdayDate.dayNumberToString(centuryAnchorNumber); return DoomsdayDate.getWeekDayOf(centuryAnchorNumber);
}; };
/** /**
@ -268,7 +268,7 @@ class DoomsdayDate {
const anchorDate = new Date(this.date); const anchorDate = new Date(this.date);
anchorDate.setDate(4); // 4th anchorDate.setDate(4); // 4th
anchorDate.setMonth(3); // April anchorDate.setMonth(3); // April
return DoomsdayDate.dayNumberToString(anchorDate.getDay()); return DoomsdayDate.getWeekDayOf(anchorDate);
}; };
/** /**
@ -277,32 +277,36 @@ class DoomsdayDate {
* @return {string} the day of the week of this `DoomsdayDate` * @return {string} the day of the week of this `DoomsdayDate`
*/ */
getWeekdayString() { getWeekdayString() {
return DoomsdayDate.dayNumberToString(this.date.getDay()); return DoomsdayDate.getWeekDayOf(this.date);
}; };
/** /**
* Returns the name of the day given its 0-based index, where 0 is `Sunday`. * Returns the week day of [date].
* *
* @param dayNumber {number} the number of the day, as returned by `Date`'s `#getDay` function. * @param date {Date|number} the date to get the week day of; if it is a `number`, then 0 corresponds to Sunday
* @return {string} the name of the day given its 0-based index, where 0 is `Sunday` * @return {string} the name of the week day corresponding to [date]
*/ */
static dayNumberToString(dayNumber) { static getWeekDayOf(date) {
switch (dayNumber % 7) { if (date instanceof Date) {
case 0: return date.toLocaleString("en-US", {timeZone: "GMT", weekday: "long"});
return "Sunday"; } else {
case 1: switch (date % 7) {
return "Monday"; case 0:
case 2: return "Sunday";
return "Tuesday"; case 1:
case 3: return "Monday";
return "Wednesday"; case 2:
case 4: return "Tuesday";
return "Thursday"; case 3:
case 5: return "Wednesday";
return "Friday"; case 4:
case 6: return "Thursday";
return "Saturday"; case 5:
return "Friday";
case 6:
return "Saturday";
}
} }
}; };
@ -341,7 +345,7 @@ class DoomsdayDate {
* @return {DoomsdayDate} a random date * @return {DoomsdayDate} a random date
*/ */
static random() { static random() {
// TODO Give custom dates to this method // TODO Give custom date range to this method
const minDate = new Date("0001-01-01").getTime() / 86400000; const minDate = new Date("0001-01-01").getTime() / 86400000;
const maxDate = new Date("9999-12-31").getTime() / 86400000; const maxDate = new Date("9999-12-31").getTime() / 86400000;
return new DoomsdayDate(new Date(generateRandom(minDate, maxDate) * 86400000)); return new DoomsdayDate(new Date(generateRandom(minDate, maxDate) * 86400000));
@ -475,6 +479,7 @@ doAfterLoad(() => {
quizDate = DoomsdayDate.random(); quizDate = DoomsdayDate.random();
console.log("# Reset"); console.log("# Reset");
console.log(`New date: ${quizDate.date.toISOString().substr(0, 10)}`); console.log(`New date: ${quizDate.date.toISOString().substr(0, 10)}`);
console.log(` ${quizDate.date}`);
console.log(`Century#: ${quizDate.getCentury()}`); console.log(`Century#: ${quizDate.getCentury()}`);
console.log(`Century: ${quizDate.getCenturyAnchorString()}`); console.log(`Century: ${quizDate.getCenturyAnchorString()}`);
console.log(`Year: ${quizDate.getYearAnchorString()}`); console.log(`Year: ${quizDate.getYearAnchorString()}`);