mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-28 05:37:34 +03:00
Fixed time difference calculation in DST timezones
refs https://github.com/TryGhost/Team/issues/588 - date-fns proved to be unable to manipulate dates consistently in UTC timezone. Keeping all calculations and formatting in UTC is key to have consistency in dates when dealing in inter-system dates - day.js also failed the test for correct UTC manipulation. See https://github.com/iamkun/dayjs/issues/1271 for example bug which prevents from consistent correct calculation - luxon was the best option which WORKED. It's also a recommended successor for moment.js with really nice docs and active support
This commit is contained in:
parent
16e21236ba
commit
6a1e722648
@ -1,6 +1,4 @@
|
||||
const differenceInMonths = require('date-fns/differenceInMonths');
|
||||
const parseISO = require('date-fns/parseISO');
|
||||
const addMonths = require('date-fns/addMonths');
|
||||
const {DateTime} = require('luxon');
|
||||
|
||||
const SUPPORTED_INTERVALS = ['month'];
|
||||
/**
|
||||
@ -14,11 +12,13 @@ const SUPPORTED_INTERVALS = ['month'];
|
||||
*/
|
||||
const lastPeriodStart = (startDate, interval) => {
|
||||
if (interval === 'month') {
|
||||
const startDateISO = parseISO(startDate);
|
||||
const fullPeriodsPast = differenceInMonths(new Date(), startDateISO);
|
||||
const lastPeriodStartDate = addMonths(startDateISO, fullPeriodsPast);
|
||||
const startDateISO = DateTime.fromISO(startDate, {zone: 'UTC'});
|
||||
const now = DateTime.now().setZone('UTC');
|
||||
const fullPeriodsPast = Math.floor(now.diff(startDateISO, 'months').months);
|
||||
|
||||
return lastPeriodStartDate.toISOString();
|
||||
const lastPeriodStartDate = startDateISO.plus({months: fullPeriodsPast});
|
||||
|
||||
return lastPeriodStartDate.toISO();
|
||||
}
|
||||
|
||||
throw new Error('Invalid interval specified. Only "month" value is accepted.');
|
||||
|
@ -26,6 +26,7 @@
|
||||
"sinon": "10.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"lodash": "^4.17.21"
|
||||
"lodash": "^4.17.21",
|
||||
"luxon": "^1.26.0"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user