FIX: improves code, tests and utc handling of local-dates (#6644)

FIX: improves code, tests and utc handling of local-dates (#6644)

From 3ff3bb6e2adea7b7a8d47e3b05aa130b932791d7 Mon Sep 17 00:00:00 2001
From: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Date: Thu, 22 Nov 2018 17:19:24 +0100
Subject: [PATCH] FIX: improves code, tests and utc handling of local-dates
 (#6644)


diff --git a/plugins/discourse-local-dates/assets/javascripts/discourse-local-dates.js b/plugins/discourse-local-dates/assets/javascripts/discourse-local-dates.js
index aad2390..e39b843 100644
--- a/plugins/discourse-local-dates/assets/javascripts/discourse-local-dates.js
+++ b/plugins/discourse-local-dates/assets/javascripts/discourse-local-dates.js
@@ -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);
     });
diff --git a/plugins/discourse-local-dates/assets/javascripts/discourse/components/discourse-local-dates-create-form.js.es6 b/plugins/discourse-local-dates/assets/javascripts/discourse/components/discourse-local-dates-create-form.js.es6
index 6c2010b..0f65085 100644
--- a/plugins/discourse-local-dates/assets/javascripts/discourse/components/discourse-local-dates-create-form.js.es6
+++ b/plugins/discourse-local-dates/assets/javascripts/discourse/components/discourse-local-dates-create-form.js.es6
@@ -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 += `]`;
diff --git a/plugins/discourse-local-dates/assets/javascripts/lib/discourse-markdown/discourse-local-dates.js.es6 b/plugins/discourse-local-dates/assets/javascripts/lib/discourse-markdown/discourse-local-dates.js.es6
index 5b63766..231bae9 100644
--- a/plugins/discourse-local-dates/assets/javascripts/lib/discourse-markdown/discourse-local-dates.js.es6
+++ b/plugins/discourse-local-dates/assets/javascripts/lib/discourse-markdown/discourse-local-dates.js.es6
@@ -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.timezon

GitHub

2 Likes