FIX: infers time from tz creator to ensure day shown is the one expected

This commit is contained in:
Joffrey JAFFEUX 2018-07-16 01:04:50 +02:00 committed by GitHub
parent f3b17b92b7
commit c580a39d49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 22 deletions

View File

@ -11,7 +11,10 @@
clearTimeout(this.timeout); clearTimeout(this.timeout);
} }
var relativeTime = moment.utc(options.date + " " + options.time, "YYYY-MM-DD HH:mm"); var relativeTime = moment.utc(
options.date + " " + options.time,
"YYYY-MM-DD HH:mm"
);
if (options.recurring && relativeTime < moment().utc()) { if (options.recurring && relativeTime < moment().utc()) {
var parts = options.recurring.split("."); var parts = options.recurring.split(".");
@ -24,7 +27,7 @@
} }
var previews = options.timezones.split("|").map(function(timezone) { var previews = options.timezones.split("|").map(function(timezone) {
var dateTime = relativeTime.tz(timezone).format(options.format); var dateTime = relativeTime.tz(timezone).format("LLL");
var timezoneParts = _formatTimezone(timezone); var timezoneParts = _formatTimezone(timezone);
if (dateTime.match(/TZ/)) { if (dateTime.match(/TZ/)) {
@ -43,10 +46,13 @@
var html = "<span>"; var html = "<span>";
html += "<i class='fa fa-globe d-icon d-icon-globe'></i>"; html += "<i class='fa fa-globe d-icon d-icon-globe'></i>";
html += relativeTime.replace("TZ", _formatTimezone(moment.tz.guess()).join(": ")); html += relativeTime.replace(
"TZ",
_formatTimezone(moment.tz.guess()).join(": ")
);
html += "</span>"; html += "</span>";
var joinedPreviews = previews.join("\n"); var joinedPreviews = previews.join(" ");
$element $element
.html(html) .html(html)

View File

@ -28,7 +28,9 @@ export default Ember.Component.extend({
); );
this.set( this.set(
"formats", "formats",
(this.siteSettings.discourse_local_dates_default_formats || "").split("|") (this.siteSettings.discourse_local_dates_default_formats || "")
.split("|")
.filter(f => f)
); );
}, },
@ -94,20 +96,28 @@ export default Ember.Component.extend({
const recurring = this.get("recurring"); const recurring = this.get("recurring");
const format = this.get("format"); const format = this.get("format");
const timezones = this.get("timezones"); const timezones = this.get("timezones");
const timeInferred = time ? false : true;
const toTimeInferred = toTime ? false : true;
let dateTime; let dateTime;
if (!timeInferred) {
if (time) { dateTime = moment
dateTime = moment(`${date} ${time}`, this.dateTimeFormat).utc(); .tz(`${date} ${time}`, this.get("currentUserTimezone"))
.utc();
} else { } else {
dateTime = moment(date, this.dateFormat).startOf("day"); dateTime = moment.tz(date, this.get("currentUserTimezone")).utc();
} }
let toDateTime; let toDateTime;
if (toTime) { if (!toTimeInferred) {
toDateTime = moment(`${toDate} ${toTime}`, this.dateTimeFormat).utc(); toDateTime = moment
.tz(`${toDate} ${toTime}`, this.get("currentUserTimezone"))
.utc();
} else { } else {
toDateTime = moment(toDate, this.dateFormat).endOf("day"); toDateTime = moment
.tz(toDate, this.get("currentUserTimezone"))
.endOf("day")
.utc();
} }
let config = { let config = {
@ -118,19 +128,18 @@ export default Ember.Component.extend({
timezones timezones
}; };
if (time) { config.time = dateTime.format(this.timeFormat);
config.time = dateTime.format(this.timeFormat); config.toTime = toDateTime.format(this.timeFormat);
}
if (toDate) { if (toDate) {
config.toDate = toDateTime.format(this.dateFormat); config.toDate = toDateTime.format(this.dateFormat);
} }
if (toTime) { if (
config.toTime = toDateTime.format(this.timeFormat); timeInferred &&
} toTimeInferred &&
this.get("formats").includes(format)
if (!time && !toTime && this.get("formats").includes(format)) { ) {
config.format = "LL"; config.format = "LL";
} }
@ -139,8 +148,8 @@ export default Ember.Component.extend({
} }
if ( if (
time && !timeInferred &&
toTime && !toTimeInferred &&
date === moment().format(this.dateFormat) && date === moment().format(this.dateFormat) &&
date === toDate && date === toDate &&
this.get("formats").includes(format) this.get("formats").includes(format)