From 96f6adecac36915458c390325e7e2623ca1ee6ff Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Mon, 14 Oct 2024 13:18:05 +0200 Subject: [PATCH] Stripped moment from `data-manipulation.fixDatesWhenFetch` - we don't need moment here and we can produce the same result in JS Date, which is a lot faster than moment --- .../server/models/base/plugins/data-manipulation.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ghost/core/core/server/models/base/plugins/data-manipulation.js b/ghost/core/core/server/models/base/plugins/data-manipulation.js index c447a3e2c4..3b2add4dcf 100644 --- a/ghost/core/core/server/models/base/plugins/data-manipulation.js +++ b/ghost/core/core/server/models/base/plugins/data-manipulation.js @@ -54,14 +54,19 @@ module.exports = function (Bookshelf) { Object.keys(attrs).forEach((key) => { if (attrs[key] && tableDef?.[key]?.type === 'dateTime') { - const dateMoment = moment(attrs[key]); + const dateValue = new Date(attrs[key]); // CASE: You are somehow able to store e.g. 0000-00-00 00:00:00 // Protect the code base and return the current date time. - if (dateMoment.isValid()) { - attrs[key] = dateMoment.startOf('seconds').toDate(); + if (!isNaN(dateValue.getTime())) { + // Valid date: set to the start of the second + dateValue.setMilliseconds(0); + attrs[key] = dateValue; } else { - attrs[key] = moment().startOf('seconds').toDate(); + // Invalid date: use current date-time + const currentDate = new Date(); + currentDate.setMilliseconds(0); + attrs[key] = currentDate; } } });