0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

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
This commit is contained in:
Daniel Lockyer 2024-10-14 13:18:05 +02:00 committed by Daniel Lockyer
parent 7bd70a3ab2
commit 96f6adecac

View file

@ -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;
}
}
});