mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: better handling of same offset timezones (#6680)
This commit is contained in:
@@ -120,10 +120,14 @@
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const compareZones = (timezoneA, timezoneB) => {
|
||||||
|
return (
|
||||||
|
moment.tz(timezoneA).utcOffset() === moment.tz(timezoneB).utcOffset()
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const _applyFormatting = (dateTime, displayedTimezone, options) => {
|
const _applyFormatting = (dateTime, displayedTimezone, options) => {
|
||||||
const sameTimezone =
|
const sameTimezone = compareZones(displayedTimezone, moment.tz.guess());
|
||||||
moment.tz(displayedTimezone).utcOffset() ===
|
|
||||||
moment.tz(moment.tz.guess()).utcOffset();
|
|
||||||
const inCalendarRange = dateTime.isBetween(
|
const inCalendarRange = dateTime.isBetween(
|
||||||
moment().subtract(2, "days"),
|
moment().subtract(2, "days"),
|
||||||
moment().add(2, "days")
|
moment().add(2, "days")
|
||||||
@@ -196,8 +200,11 @@
|
|||||||
const _generatePreviews = (dateTime, displayedTimezone, options) => {
|
const _generatePreviews = (dateTime, displayedTimezone, options) => {
|
||||||
const previewedTimezones = [];
|
const previewedTimezones = [];
|
||||||
const watchingUserTimezone = moment.tz.guess();
|
const watchingUserTimezone = moment.tz.guess();
|
||||||
|
const timezones = options.timezones.filter(
|
||||||
|
timezone => timezone !== watchingUserTimezone
|
||||||
|
);
|
||||||
|
|
||||||
if (displayedTimezone !== watchingUserTimezone) {
|
if (!compareZones(displayedTimezone, watchingUserTimezone)) {
|
||||||
previewedTimezones.push({
|
previewedTimezones.push({
|
||||||
timezone: watchingUserTimezone,
|
timezone: watchingUserTimezone,
|
||||||
current: true,
|
current: true,
|
||||||
@@ -207,18 +214,41 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
options.timezones
|
if (
|
||||||
.filter(x => x !== watchingUserTimezone)
|
displayedTimezone === watchingUserTimezone &&
|
||||||
.forEach(timezone => {
|
options.timezone !== displayedTimezone &&
|
||||||
previewedTimezones.push({
|
!compareZones(displayedTimezone, options.timezone)
|
||||||
timezone,
|
) {
|
||||||
dateTime: options.time
|
timezones.unshift(options.timezone);
|
||||||
? dateTime.tz(timezone).format("LLL")
|
}
|
||||||
: createDateTimeRange(dateTime, timezone)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return previewedTimezones;
|
timezones.forEach(timezone => {
|
||||||
|
if (compareZones(timezone, displayedTimezone)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (compareZones(timezone, watchingUserTimezone)) {
|
||||||
|
timezone = watchingUserTimezone;
|
||||||
|
}
|
||||||
|
|
||||||
|
previewedTimezones.push({
|
||||||
|
timezone,
|
||||||
|
dateTime: options.time
|
||||||
|
? dateTime.tz(timezone).format("LLL")
|
||||||
|
: createDateTimeRange(dateTime, timezone)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!previewedTimezones.length) {
|
||||||
|
previewedTimezones.push({
|
||||||
|
timezone: "Etc/UTC",
|
||||||
|
dateTime: options.time
|
||||||
|
? dateTime.tz("Etc/UTC").format("LLL")
|
||||||
|
: createDateTimeRange(dateTime, "Etc/UTC")
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return _.uniq(previewedTimezones, "timezone");
|
||||||
};
|
};
|
||||||
|
|
||||||
const _generateTextPreview = previews => {
|
const _generateTextPreview = previews => {
|
||||||
|
|||||||
@@ -4,7 +4,10 @@ const sandbox = sinon.createSandbox();
|
|||||||
|
|
||||||
acceptance("Local Dates", {
|
acceptance("Local Dates", {
|
||||||
loggedIn: true,
|
loggedIn: true,
|
||||||
settings: { discourse_local_dates_enabled: true },
|
settings: {
|
||||||
|
discourse_local_dates_enabled: true,
|
||||||
|
discourse_local_dates_default_timezones: "Europe/Paris|America/Los_Angeles"
|
||||||
|
},
|
||||||
beforeEach() {
|
beforeEach() {
|
||||||
freezeDateAndZone();
|
freezeDateAndZone();
|
||||||
},
|
},
|
||||||
@@ -56,6 +59,7 @@ function generateHTML(options = {}) {
|
|||||||
|
|
||||||
output += ` data-date="${options.date || DEFAULT_DATE}"`;
|
output += ` data-date="${options.date || DEFAULT_DATE}"`;
|
||||||
if (options.format) output += ` data-format="${options.format}"`;
|
if (options.format) output += ` data-format="${options.format}"`;
|
||||||
|
if (options.timezones) output += ` data-timezones="${options.timezones}"`;
|
||||||
if (options.time) output += ` data-time="${options.time}"`;
|
if (options.time) output += ` data-time="${options.time}"`;
|
||||||
output += ` data-timezone="${options.timezone || DEFAULT_ZONE}"`;
|
output += ` data-timezone="${options.timezone || DEFAULT_ZONE}"`;
|
||||||
if (options.calendar) output += ` data-calendar="${options.calendar}"`;
|
if (options.calendar) output += ` data-calendar="${options.calendar}"`;
|
||||||
@@ -319,7 +323,6 @@ test("displayedTimezone", assert => {
|
|||||||
test("tooltip", assert => {
|
test("tooltip", assert => {
|
||||||
let html = generateHTML({ timezone: "America/Chicago" });
|
let html = generateHTML({ timezone: "America/Chicago" });
|
||||||
let transformed = $(html).applyLocalDates();
|
let transformed = $(html).applyLocalDates();
|
||||||
|
|
||||||
let htmlToolip = transformed.attr("data-html-tooltip");
|
let htmlToolip = transformed.attr("data-html-tooltip");
|
||||||
let currentUserPreview = $(htmlToolip).find(".preview.current");
|
let currentUserPreview = $(htmlToolip).find(".preview.current");
|
||||||
let timezone = currentUserPreview.find(".timezone").text();
|
let timezone = currentUserPreview.find(".timezone").text();
|
||||||
@@ -348,26 +351,83 @@ test("tooltip", assert => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
html = generateHTML({ timezone: "America/Chicago", time: "14:00:00" });
|
html = generateHTML({
|
||||||
|
timezones: "Etc/UTC",
|
||||||
|
timezone: "America/Chicago",
|
||||||
|
time: "14:00:00"
|
||||||
|
});
|
||||||
transformed = $(html).applyLocalDates();
|
transformed = $(html).applyLocalDates();
|
||||||
htmlToolip = transformed.attr("data-html-tooltip");
|
htmlToolip = transformed.attr("data-html-tooltip");
|
||||||
|
|
||||||
const $preview = $(htmlToolip)
|
|
||||||
.find(".preview")
|
|
||||||
.first();
|
|
||||||
dateTime = $preview.find(".date-time").text();
|
|
||||||
timezone = $preview.find(".timezone").text();
|
|
||||||
|
|
||||||
assert.ok(
|
assert.ok(
|
||||||
!exists(".preview.current"),
|
!exists(".preview.current"),
|
||||||
"doesn’t create current timezone when displayed timezone equals watching user timezone"
|
"doesn’t create current timezone when displayed timezone equals watching user timezone"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let $firstPreview = $(htmlToolip).find(".preview:nth-child(1)");
|
||||||
|
dateTime = $firstPreview.find(".date-time").text();
|
||||||
|
timezone = $firstPreview.find(".timezone").text();
|
||||||
|
assert.equal(
|
||||||
|
dateTime,
|
||||||
|
"June 20, 2018 2:00 PM",
|
||||||
|
"it doesn’t create range if time has been set"
|
||||||
|
);
|
||||||
|
assert.equal(timezone, "Chicago", "it adds the timezone of the creator");
|
||||||
|
|
||||||
|
let $secondPreview = $(htmlToolip).find(".preview:nth-child(2)");
|
||||||
|
dateTime = $secondPreview.find(".date-time").text();
|
||||||
|
timezone = $secondPreview.find(".timezone").text();
|
||||||
assert.equal(
|
assert.equal(
|
||||||
dateTime,
|
dateTime,
|
||||||
"June 20, 2018 7:00 PM",
|
"June 20, 2018 7:00 PM",
|
||||||
"it doesn’t create range if time has been set"
|
"it doesn’t create range if time has been set"
|
||||||
);
|
);
|
||||||
assert.equal(timezone, "UTC", "Etc/UTC is rewritten to UTC");
|
assert.equal(timezone, "UTC", "Etc/UTC is rewritten to UTC");
|
||||||
|
|
||||||
|
freezeDateAndZone(moment("2018-11-26 21:00:00"), "Europe/Vienna", () => {
|
||||||
|
html = generateHTML({
|
||||||
|
date: "2018-11-22",
|
||||||
|
timezone: "America/Chicago",
|
||||||
|
time: "14:00"
|
||||||
|
});
|
||||||
|
transformed = $(html).applyLocalDates();
|
||||||
|
htmlToolip = transformed.attr("data-html-tooltip");
|
||||||
|
|
||||||
|
$firstPreview = $(htmlToolip)
|
||||||
|
.find(".preview")
|
||||||
|
.first();
|
||||||
|
|
||||||
|
assert.equal(
|
||||||
|
$firstPreview.find(".timezone").text(),
|
||||||
|
"Chicago",
|
||||||
|
"it adds the creator timezone to the previews"
|
||||||
|
);
|
||||||
|
assert.equal(
|
||||||
|
$firstPreview.find(".date-time").text(),
|
||||||
|
"November 22, 2018 2:00 PM",
|
||||||
|
"it adds the creator timezone to the previews"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
freezeDateAndZone(DEFAULT_DATE, "Europe/Vienna", () => {
|
||||||
|
html = generateHTML({
|
||||||
|
date: "2018-11-22",
|
||||||
|
timezone: "America/Chicago",
|
||||||
|
timezones: "Europe/Paris"
|
||||||
|
});
|
||||||
|
transformed = $(html).applyLocalDates();
|
||||||
|
htmlToolip = transformed.attr("data-html-tooltip");
|
||||||
|
|
||||||
|
$firstPreview = $(htmlToolip)
|
||||||
|
.find(".preview")
|
||||||
|
.first();
|
||||||
|
|
||||||
|
assert.equal(
|
||||||
|
$firstPreview.find(".timezone").text(),
|
||||||
|
"Vienna",
|
||||||
|
"it rewrites timezone with same offset and different name than watching user"
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test("test utils", assert => {
|
test("test utils", assert => {
|
||||||
|
|||||||
Reference in New Issue
Block a user