mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 22:43:33 +08:00
FIX: improves code, tests and utc handling of local-dates (#6644)
This commit is contained in:
@ -12,12 +12,16 @@
|
||||
}
|
||||
|
||||
var relativeTime;
|
||||
|
||||
var dateAndTime = options.date;
|
||||
if (options.time) {
|
||||
dateAndTime = dateAndTime + " " + options.time;
|
||||
}
|
||||
|
||||
if (options.timezone) {
|
||||
relativeTime = moment
|
||||
.tz(options.date + " " + options.time, options.timezone)
|
||||
.utc();
|
||||
relativeTime = moment.tz(dateAndTime, options.timezone).utc();
|
||||
} else {
|
||||
relativeTime = moment.utc(options.date + " " + options.time);
|
||||
relativeTime = moment.utc(dateAndTime);
|
||||
}
|
||||
|
||||
if (relativeTime < moment().utc()) {
|
||||
@ -35,9 +39,7 @@
|
||||
}
|
||||
|
||||
var previews = options.timezones.split("|").map(function(timezone) {
|
||||
var dateTime = relativeTime
|
||||
.tz(timezone)
|
||||
.format(options.format || "LLL");
|
||||
var dateTime = relativeTime.tz(timezone).format(options.format);
|
||||
|
||||
var timezoneParts = _formatTimezone(timezone);
|
||||
|
||||
@ -53,20 +55,23 @@
|
||||
}
|
||||
});
|
||||
|
||||
var displayTimezone = moment.tz.guess();
|
||||
var relativeTime = relativeTime.tz(displayTimezone);
|
||||
var relativeTime = relativeTime.tz(options.displayedZone);
|
||||
|
||||
var d = function(key) {
|
||||
var translated = I18n.t("discourse_local_dates.relative_dates." + key, {
|
||||
time: "LT"
|
||||
});
|
||||
translated = translated
|
||||
.split("LT")
|
||||
.map(function(w) {
|
||||
return "[" + w + "]";
|
||||
})
|
||||
.join("LT");
|
||||
return translated;
|
||||
|
||||
if (options.time) {
|
||||
return translated
|
||||
.split("LT")
|
||||
.map(function(w) {
|
||||
return "[" + w + "]";
|
||||
})
|
||||
.join("LT");
|
||||
} else {
|
||||
return "[" + translated.replace(" LT", "") + "]";
|
||||
}
|
||||
};
|
||||
|
||||
var relativeFormat = {
|
||||
@ -77,7 +82,7 @@
|
||||
};
|
||||
|
||||
if (
|
||||
options.format !== "YYYY-MM-DD HH:mm:ss" &&
|
||||
options.calendar &&
|
||||
relativeTime.isBetween(
|
||||
moment().subtract(1, "day"),
|
||||
moment().add(2, "day")
|
||||
@ -97,7 +102,7 @@
|
||||
|
||||
var displayedTime = relativeTime.replace(
|
||||
"TZ",
|
||||
_formatTimezone(displayTimezone).join(": ")
|
||||
_formatTimezone(options.displayedZone).join(": ")
|
||||
);
|
||||
|
||||
$element
|
||||
@ -119,12 +124,16 @@
|
||||
var $this = $(this);
|
||||
|
||||
var options = {};
|
||||
options.format = $this.attr("data-format");
|
||||
options.time = $this.attr("data-time");
|
||||
options.format =
|
||||
$this.attr("data-format") || (options.time ? "LLL" : "LL");
|
||||
options.date = $this.attr("data-date");
|
||||
options.time = $this.attr("data-time") || "00:00:00";
|
||||
options.recurring = $this.attr("data-recurring");
|
||||
options.timezones = $this.attr("data-timezones");
|
||||
options.timezones = $this.attr("data-timezones") || "Etc/UTC";
|
||||
options.timezone = $this.attr("data-timezone");
|
||||
options.calendar = ($this.attr("data-calendar") || "on") === "on";
|
||||
options.displayedZone =
|
||||
$this.attr("data-displayed-zone") || moment.tz.guess();
|
||||
|
||||
processElement($this, options);
|
||||
});
|
||||
|
@ -102,9 +102,9 @@ export default Ember.Component.extend({
|
||||
|
||||
let dateTime;
|
||||
if (!timeInferred) {
|
||||
dateTime = moment.tz(`${date} ${time}`, timezone);
|
||||
dateTime = moment.tz(`${date} ${time}`, timezone).utc();
|
||||
} else {
|
||||
dateTime = moment.tz(date, timezone);
|
||||
dateTime = moment.tz(date, timezone).utc();
|
||||
}
|
||||
|
||||
let toDateTime;
|
||||
@ -123,8 +123,13 @@ export default Ember.Component.extend({
|
||||
timezone
|
||||
};
|
||||
|
||||
config.time = dateTime.format(this.timeFormat);
|
||||
config.toTime = toDateTime.format(this.timeFormat);
|
||||
if (!timeInferred) {
|
||||
config.time = dateTime.format(this.timeFormat);
|
||||
}
|
||||
|
||||
if (!toTimeInferred) {
|
||||
config.toTime = toDateTime.format(this.timeFormat);
|
||||
}
|
||||
|
||||
if (toDate) {
|
||||
config.toDate = toDateTime.format(this.dateFormat);
|
||||
@ -163,7 +168,6 @@ export default Ember.Component.extend({
|
||||
text += `time=${config.time} `;
|
||||
}
|
||||
|
||||
text += `timezone="${config.timezone}" `;
|
||||
text += `format="${config.format}" `;
|
||||
text += `timezones="${config.timezones.join("|")}"`;
|
||||
text += `]`;
|
||||
@ -176,7 +180,6 @@ export default Ember.Component.extend({
|
||||
text += `time=${config.toTime} `;
|
||||
}
|
||||
|
||||
text += `timezone="${config.timezone}" `;
|
||||
text += `format="${config.format}" `;
|
||||
text += `timezones="${config.timezones.join("|")}"`;
|
||||
text += `]`;
|
||||
|
@ -7,8 +7,9 @@ function addLocalDate(buffer, matches, state) {
|
||||
date: null,
|
||||
time: null,
|
||||
timezone: null,
|
||||
format: "YYYY-MM-DD HH:mm:ss",
|
||||
timezones: "Etc/UTC"
|
||||
format: null,
|
||||
timezones: null,
|
||||
displayedZone: null
|
||||
};
|
||||
|
||||
let parsed = parseBBCodeTag(
|
||||
@ -18,18 +19,18 @@ function addLocalDate(buffer, matches, state) {
|
||||
);
|
||||
|
||||
config.date = parsed.attrs.date;
|
||||
config.format = parsed.attrs.format;
|
||||
config.calendar = parsed.attrs.calendar;
|
||||
config.time = parsed.attrs.time;
|
||||
config.timezone = parsed.attrs.timezone;
|
||||
config.recurring = parsed.attrs.recurring;
|
||||
config.format = parsed.attrs.format || config.format;
|
||||
config.timezones = parsed.attrs.timezones || config.timezones;
|
||||
config.timezones = parsed.attrs.timezones;
|
||||
config.displayedZone = parsed.attrs.displayedZone;
|
||||
|
||||
token = new state.Token("span_open", "span", 1);
|
||||
token.attrs = [
|
||||
["class", "discourse-local-date"],
|
||||
["data-date", state.md.utils.escapeHtml(config.date)],
|
||||
["data-format", state.md.utils.escapeHtml(config.format)],
|
||||
["data-timezones", state.md.utils.escapeHtml(config.timezones)]
|
||||
["data-date", state.md.utils.escapeHtml(config.date)]
|
||||
];
|
||||
|
||||
let dateTime = config.date;
|
||||
@ -38,6 +39,31 @@ function addLocalDate(buffer, matches, state) {
|
||||
dateTime = `${dateTime} ${config.time}`;
|
||||
}
|
||||
|
||||
if (config.format) {
|
||||
token.attrs.push(["data-format", state.md.utils.escapeHtml(config.format)]);
|
||||
}
|
||||
|
||||
if (config.calendar) {
|
||||
token.attrs.push([
|
||||
"data-calendar",
|
||||
state.md.utils.escapeHtml(config.calendar)
|
||||
]);
|
||||
}
|
||||
|
||||
if (config.displayedZone) {
|
||||
token.attrs.push([
|
||||
"data-displayed-zone",
|
||||
state.md.utils.escapeHtml(config.displayedZone)
|
||||
]);
|
||||
}
|
||||
|
||||
if (config.timezones) {
|
||||
token.attrs.push([
|
||||
"data-timezones",
|
||||
state.md.utils.escapeHtml(config.timezones)
|
||||
]);
|
||||
}
|
||||
|
||||
if (config.timezone) {
|
||||
token.attrs.push([
|
||||
"data-timezone",
|
||||
@ -54,10 +80,11 @@ function addLocalDate(buffer, matches, state) {
|
||||
state.md.utils.escapeHtml(config.recurring)
|
||||
]);
|
||||
}
|
||||
|
||||
buffer.push(token);
|
||||
|
||||
let emailPreview;
|
||||
const emailTimezone = config.timezones.split("|")[0];
|
||||
const emailTimezone = (config.timezones || "Etc/UTC").split("|")[0];
|
||||
const formattedDateTime = dateTime.tz(emailTimezone).format(config.format);
|
||||
const formattedTimezone = emailTimezone.replace("/", ": ").replace("_", " ");
|
||||
|
||||
@ -66,7 +93,6 @@ function addLocalDate(buffer, matches, state) {
|
||||
} else {
|
||||
emailPreview = `${formattedDateTime} (${formattedTimezone})`;
|
||||
}
|
||||
|
||||
token.attrs.push(["data-email-preview", emailPreview]);
|
||||
|
||||
token = new state.Token("text", "", 0);
|
||||
@ -74,6 +100,7 @@ function addLocalDate(buffer, matches, state) {
|
||||
buffer.push(token);
|
||||
|
||||
token = new state.Token("span_close", "span", -1);
|
||||
|
||||
buffer.push(token);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user