mirror of
https://github.com/discourse/discourse.git
synced 2025-06-10 03:53:41 +08:00
FIX: improves recurring by ensuring DST is computed correctly (#8410)
This commit is contained in:
@ -22,7 +22,6 @@
|
|||||||
const dateTime = options.time
|
const dateTime = options.time
|
||||||
? `${options.date} ${options.time}`
|
? `${options.date} ${options.time}`
|
||||||
: options.date;
|
: options.date;
|
||||||
let utcDateTime;
|
|
||||||
|
|
||||||
let displayedTimezone;
|
let displayedTimezone;
|
||||||
if (options.time) {
|
if (options.time) {
|
||||||
@ -33,6 +32,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
// if timezone given we convert date and time from given zone to Etc/UTC
|
// if timezone given we convert date and time from given zone to Etc/UTC
|
||||||
|
let utcDateTime;
|
||||||
if (options.timezone) {
|
if (options.timezone) {
|
||||||
utcDateTime = _applyZoneToDateTime(dateTime, options.timezone);
|
utcDateTime = _applyZoneToDateTime(dateTime, options.timezone);
|
||||||
} else {
|
} else {
|
||||||
@ -43,7 +43,7 @@
|
|||||||
// if event is in the past we want to bump it no next occurrence when
|
// if event is in the past we want to bump it no next occurrence when
|
||||||
// recurring is set
|
// recurring is set
|
||||||
if (options.recurring) {
|
if (options.recurring) {
|
||||||
utcDateTime = _applyRecurrence(utcDateTime, options.recurring);
|
utcDateTime = _applyRecurrence(utcDateTime, options);
|
||||||
} else {
|
} else {
|
||||||
$element.addClass("past");
|
$element.addClass("past");
|
||||||
}
|
}
|
||||||
@ -199,22 +199,37 @@
|
|||||||
return dateTime;
|
return dateTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _applyRecurrence(dateTime, recurring) {
|
function _applyRecurrence(dateTime, { recurring, timezone }) {
|
||||||
const parts = recurring.split(".");
|
const parts = recurring.split(".");
|
||||||
const count = parseInt(parts[0], 10);
|
const count = parseInt(parts[0], 10);
|
||||||
const type = parts[1];
|
const type = parts[1];
|
||||||
const diff = moment().diff(dateTime, type);
|
const diff = moment().diff(dateTime, type);
|
||||||
const add = Math.ceil(diff + count);
|
const add = Math.ceil(diff + count);
|
||||||
const wasDST = dateTime.isDST();
|
|
||||||
let dateTimeWithRecurrence = dateTime.add(add, type);
|
|
||||||
const isDST = dateTimeWithRecurrence.isDST();
|
|
||||||
|
|
||||||
|
// we create new moment object from format
|
||||||
|
// to ensure it's created in user context
|
||||||
|
const wasDST = moment(dateTime.format()).isDST();
|
||||||
|
let dateTimeWithRecurrence = moment(dateTime).add(add, type);
|
||||||
|
const isDST = moment(dateTimeWithRecurrence.format()).isDST();
|
||||||
|
|
||||||
|
// these dates are more or less DST "certain"
|
||||||
|
const noDSTOffset = moment
|
||||||
|
.tz({ month: 0, day: 1 }, timezone || "Etc/UTC")
|
||||||
|
.utcOffset();
|
||||||
|
const withDSTOffset = moment
|
||||||
|
.tz({ month: 5, day: 1 }, timezone || "Etc/UTC")
|
||||||
|
.utcOffset();
|
||||||
|
|
||||||
|
// we remove the DST offset present when the date was created,
|
||||||
|
// and add current DST offset
|
||||||
if (!wasDST && isDST) {
|
if (!wasDST && isDST) {
|
||||||
dateTimeWithRecurrence.subtract(1, "hour");
|
dateTimeWithRecurrence.add(-withDSTOffset + noDSTOffset, "minutes");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// we add the DST offset present when the date was created,
|
||||||
|
// and remove current DST offset
|
||||||
if (wasDST && !isDST) {
|
if (wasDST && !isDST) {
|
||||||
dateTimeWithRecurrence.add(1, "hour");
|
dateTimeWithRecurrence.add(withDSTOffset - noDSTOffset, "minutes");
|
||||||
}
|
}
|
||||||
|
|
||||||
return dateTimeWithRecurrence;
|
return dateTimeWithRecurrence;
|
||||||
|
Reference in New Issue
Block a user