Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial for #3252: new Date() -> Date.now() #3314

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion js/server_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions modules/default/calendar/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -906,7 +906,7 @@ Module.register("calendar", {
}
}, ONE_MINUTE);
},
ONE_MINUTE - (new Date() % ONE_MINUTE)
ONE_MINUTE - (Date.now() % ONE_MINUTE)
);
}
});
8 changes: 4 additions & 4 deletions modules/default/calendar/calendarfetcherutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion modules/default/calendar/calendarutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion modules/default/weather/providers/weatherbit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Date.now() returns the number of milliseconds since January 1, 1970. methods like getTimezoneOffset will not work with your change.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or those in calendarUtils

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Fixed up in latest push

tzOffset = tzOffset * -1;

Expand Down
2 changes: 1 addition & 1 deletion modules/default/weather/weatherobject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/modules/default/calendar/calendar_utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/modules/default/utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down