diff --git a/CHANGELOG.md b/CHANGELOG.md index 45cde47017..d50acaf26d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ _This release is scheduled to be released on 2024-01-01._ - electron is now per default started without gpu, if needed it must be enabled with new env var `ELECTRON_ENABLE_GPU=1` on startup (#3226) - Replace prettier by stylistic in ESLint config to lint JavaScript (and disable some rules for `config/config.js*` files) - Update node-ical to v0.17.1 and fix tests +- Changed `new Date()` to `Date.now()` to aid debugging (#3252) ### Fixed diff --git a/js/server_functions.js b/js/server_functions.js index c975df7c5e..7509b2945e 100644 --- a/js/server_functions.js +++ b/js/server_functions.js @@ -2,7 +2,7 @@ const fs = require("fs"); const path = require("path"); const Log = require("logger"); -const startUp = new Date(); +const startUp = new Date(Date.now()); /** * Gets the config. diff --git a/modules/default/calendar/calendar.js b/modules/default/calendar/calendar.js index bfb49af78e..c420691bd8 100644 --- a/modules/default/calendar/calendar.js +++ b/modules/default/calendar/calendar.js @@ -400,7 +400,7 @@ Module.register("calendar", { const timeWrapper = document.createElement("td"); eventWrapper.appendChild(titleWrapper); - const now = new Date(); + const now = new Date(Date.now()); if (this.config.timeFormat === "absolute") { // Use dateFormat @@ -573,7 +573,7 @@ Module.register("calendar", { const ONE_HOUR = ONE_MINUTE * 60; const ONE_DAY = ONE_HOUR * 24; - const now = new Date(); + const now = new Date(); // new Date(Date.now()); // breaks tests const today = moment().startOf("day"); const future = moment().startOf("day").add(this.config.maximumNumberOfDays, "days").toDate(); let events = []; @@ -906,7 +906,7 @@ Module.register("calendar", { } }, ONE_MINUTE); }, - ONE_MINUTE - (new Date() % ONE_MINUTE) + ONE_MINUTE - (Date.now() % ONE_MINUTE) ); } }); diff --git a/modules/default/calendar/calendarfetcherutils.js b/modules/default/calendar/calendarfetcherutils.js index af48b7e795..8fbeb88853 100644 --- a/modules/default/calendar/calendarfetcherutils.js +++ b/modules/default/calendar/calendarfetcherutils.js @@ -63,7 +63,7 @@ const CalendarFetcherUtils = { event.start.tz = ""; Log.debug(`ical offset=${current_offset} date=${date}`); mm = moment(date); - let x = parseInt(moment(new Date()).utcOffset()); + let x = parseInt(moment(Date.now()).utcOffset()); Log.debug(`net mins=${current_offset * 60 - x}`); mm = mm.add(x - current_offset * 60, "minutes"); @@ -141,7 +141,7 @@ const CalendarFetcherUtils = { Log.debug(`There are ${Object.entries(data).length} calendar entries.`); Object.entries(data).forEach(([key, event]) => { Log.debug("Processing entry..."); - const now = new Date(); + const now = new Date(Date.now()); const today = moment().startOf("day").toDate(); const future = moment() @@ -323,7 +323,7 @@ const CalendarFetcherUtils = { // Get the offset of today where we are processing // This will be the correction, we need to apply. - let nowOffset = new Date().getTimezoneOffset(); + let nowOffset = new Date(Date.now()).getTimezoneOffset(); // For full day events, the time might be off from RRULE/Luxon problem // Get time zone offset of the rule calculated event let dateoffset = date.getTimezoneOffset(); @@ -479,7 +479,7 @@ const CalendarFetcherUtils = { } } else { // It's not a fullday event, and it is in the past, so skip. - if (!fullDayEvent && endDate < new Date()) { + if (!fullDayEvent && endDate < new Date(Date.now())) { return; } diff --git a/modules/default/calendar/calendarutils.js b/modules/default/calendar/calendarutils.js index 68aef52154..5eef12c461 100644 --- a/modules/default/calendar/calendarutils.js +++ b/modules/default/calendar/calendarutils.js @@ -116,7 +116,7 @@ const CalendarUtils = { if (typeof transform.yearmatchgroup !== "undefined" && transform.yearmatchgroup !== "") { const yearmatch = [...title.matchAll(needle)]; if (yearmatch[0].length >= transform.yearmatchgroup + 1 && yearmatch[0][transform.yearmatchgroup] * 1 >= 1900) { - let calcage = new Date().getFullYear() - yearmatch[0][transform.yearmatchgroup] * 1; + let calcage = new Date(Date.now()).getFullYear() - yearmatch[0][transform.yearmatchgroup] * 1; let searchstr = `$${transform.yearmatchgroup}`; replacement = replacement.replace(searchstr, calcage); } diff --git a/modules/default/weather/providers/weatherbit.js b/modules/default/weather/providers/weatherbit.js index 5bde8003bb..c1ee282794 100644 --- a/modules/default/weather/providers/weatherbit.js +++ b/modules/default/weather/providers/weatherbit.js @@ -95,7 +95,7 @@ WeatherProvider.register("weatherbit", { // Implement WeatherDay generator. generateWeatherDayFromCurrentWeather (currentWeatherData) { //Calculate TZ Offset and invert to convert Sunrise/Sunset times to Local - const d = new Date(); + const d = new Date(Date.now()); let tzOffset = d.getTimezoneOffset(); tzOffset = tzOffset * -1; diff --git a/modules/default/weather/weatherobject.js b/modules/default/weather/weatherobject.js index 33c9e669bf..90f21e4420 100644 --- a/modules/default/weather/weatherobject.js +++ b/modules/default/weather/weatherobject.js @@ -108,7 +108,7 @@ class WeatherObject { * @param {number} lon longitude */ updateSunTime (lat, lon) { - const now = !this.date ? new Date() : this.date.toDate(); + const now = !this.date ? new Date(Date.now()) : this.date.toDate(); const times = SunCalc.getTimes(now, lat, lon); this.sunrise = moment(times.sunrise); this.sunset = moment(times.sunset); diff --git a/tests/unit/modules/default/calendar/calendar_utils_spec.js b/tests/unit/modules/default/calendar/calendar_utils_spec.js index ff37299fe9..47866d826f 100644 --- a/tests/unit/modules/default/calendar/calendar_utils_spec.js +++ b/tests/unit/modules/default/calendar/calendar_utils_spec.js @@ -146,7 +146,7 @@ describe("Calendar utils tests", () => { describe("titleTransform with yearmatchgroup", () => { it("should replace the birthday and wrap nicely", () => { const transformedTitle = CalendarUtils.titleTransform("Luciella '2000", [{ search: "^([^']*) '(\\d{4})$", replace: "$1 ($2.)", yearmatchgroup: 2 }]); - const expectedResult = `Luciella (${new Date().getFullYear() - 2000}.)`; + const expectedResult = `Luciella (${new Date(Date.now()).getFullYear() - 2000}.)`; expect(transformedTitle).toBe(expectedResult); }); }); diff --git a/tests/unit/modules/default/utils_spec.js b/tests/unit/modules/default/utils_spec.js index 664db4906f..43a025f10d 100644 --- a/tests/unit/modules/default/utils_spec.js +++ b/tests/unit/modules/default/utils_spec.js @@ -105,7 +105,7 @@ describe("Default modules utils tests", () => { }); describe("formatTime", () => { - const time = new Date(); + const time = new Date(Date.now()); beforeEach(async () => { time.setHours(13, 13);