Bug 1313659 - replace nsIScriptableDateFormat in chat/. r=florian
authorSebastian Hengst <archaeopteryx@coole-files.de> and Jorg K <jorgk@jorgk.com>
Thu, 13 Apr 2017 15:30:16 +0200
changeset 21768 2bc6c38ccf68d00c629309448634dc7857653c06
parent 21767 b61e73be467cef8a7231c42c0aba1a82ddfffada
child 21769 3a4c51fcb4aa98885607679be7c4c7b5082641d4
push id13283
push usermozilla@jorgk.com
push dateThu, 13 Jul 2017 21:15:29 +0000
treeherdercomm-central@3a4c51fcb4aa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersflorian
bugs1313659
Bug 1313659 - replace nsIScriptableDateFormat in chat/. r=florian
chat/content/imtooltip.xml
chat/modules/imThemes.jsm
chat/protocols/twitter/twitter.js
im/content/accounts.js
im/content/debugLogPanel.xml
im/content/viewlog.js
mail/components/im/content/chat-messenger-overlay.js
mail/components/im/content/imAccounts.js
mail/locales/en-US/chrome/messenger/chat.properties
--- a/chat/content/imtooltip.xml
+++ b/chat/content/imtooltip.xml
@@ -482,26 +482,26 @@
          // Are we over a message?
          for (let node = elt; node; node = node.parentNode) {
            if (!node._originalMsg)
              continue;
            // It's a message, so add a date/time tooltip.
            let date = new Date(node._originalMsg.time * 1000);
            let text;
            if ((new Date()).toDateString() == date.toDateString()) {
-             text = date.toLocaleTimeString();
+             const dateTimeFormatter = Services.intl.createDateTimeFormat(undefined, {
+               timeStyle: "long"
+             });
+             text = dateTimeFormatter.format(date);
            }
            else {
-             let sdf =
-               Components.classes["@mozilla.org/intl/scriptabledateformat;1"]
-                         .getService(Ci.nsIScriptableDateFormat);
-             text = sdf.FormatDateTime("", sdf.dateFormatShort,
-               sdf.timeFormatSeconds, date.getFullYear(),
-               date.getMonth() + 1, date.getDate(),
-               date.getHours(), date.getMinutes(), date.getSeconds());
+             const dateTimeFormatter = Services.intl.createDateTimeFormat(undefined, {
+               dateStyle: "short", timeStyle: "long"
+             });
+             text = dateTimeFormatter.format(date);
            }
            // Setting the attribute on this node means that if the element
            // we are pointing at carries a title set by the prpl,
            // that title won't be overridden.
            node.setAttribute("title", text);
            break;
          }
 
--- a/chat/modules/imThemes.jsm
+++ b/chat/modules/imThemes.jsm
@@ -337,18 +337,20 @@ var headerFooterReplacements = {
     return (!aConv.isChat && (buddy = aConv.buddy) &&
             buddy.buddyIconFilename) || "incoming_icon.png";
   },
   outgoingIconPath: aConv => "outgoing_icon.png",
   timeOpened: function(aConv, aFormat) {
     let date = new Date(aConv.startDate / 1000);
     if (aFormat)
       return ToLocaleFormat(aFormat, date);
-    else
-      return date.toLocaleTimeString();
+    const timeFormatter = Services.intl.createDateTimeFormat(undefined, {
+      timeStyle: "long"
+    });
+    return timeFormatter.format(date);
   }
 };
 
 function formatAutoResponce(aTxt) {
   return Services.strings
                  .createBundle("chrome://chat/locale/conversations.properties")
                  .formatStringFromName("autoReply", [aTxt], 1);
 }
@@ -356,20 +358,28 @@ function formatAutoResponce(aTxt) {
 var statusMessageReplacements = {
   message: aMsg => "<span class=\"ib-msg-txt\">" +
                    (aMsg.autoResponse ? formatAutoResponce(aMsg.message) : aMsg.message) +
                    "</span>",
   time: function(aMsg, aFormat) {
     let date = new Date(aMsg.time * 1000);
     if (aFormat)
       return ToLocaleFormat(aFormat, date);
-    return date.toLocaleTimeString();
+    const timeFormatter = Services.intl.createDateTimeFormat(undefined, {
+      timeStyle: "long"
+    });
+    return timeFormatter.format(date);
   },
   timestamp: aMsg => aMsg.time,
-  shortTime: aMsg => (new Date(aMsg.time * 1000)).toLocaleTimeString(),
+  shortTime: function(aMsg) {
+    const timeFormatter = Services.intl.createDateTimeFormat(undefined, {
+      timeStyle: "long"
+    });
+    return timeFormatter.format(new Date(aMsg.time * 1000));
+  },
   messageClasses: function(aMsg) {
     let msgClass = [];
 
     if (aMsg.system)
       msgClass.push("event");
     else {
       msgClass.push("message");
 
--- a/chat/protocols/twitter/twitter.js
+++ b/chat/protocols/twitter/twitter.js
@@ -1144,17 +1144,22 @@ Account.prototype = {
           return _lang(aLang);
         }
         catch(e) {
           return aLang;
         }
       },
       time_zone: null,
       protected: normalizeBool,
-      created_at: aDate => (new Date(aDate)).toLocaleDateString(),
+      created_at: function(aDate) {
+        const dateFormatter = Services.intl.createDateTimeFormat(undefined, {
+          dateStyle: "short"
+        });
+        return dateFormatter.format(new Date(aDate));
+      },
       statuses_count: null,
       friends_count: null,
       followers_count: null,
       listed_count: null
     };
 
     let tooltipInfo = [];
     for (let field in kFields) {
--- a/im/content/accounts.js
+++ b/im/content/accounts.js
@@ -230,24 +230,21 @@ var gAccountManager = {
     // Reconnect the account if an exception was added.
     if (params.exceptionAdded)
       account.connect();
   },
   copyDebugLog: function am_copyDebugLog() {
     let account = this.accountList.selectedItem.account;
     let text = account.getDebugMessages().map(function(dbgMsg) {
       let m = dbgMsg.message;
-      const dateServ = Cc["@mozilla.org/intl/scriptabledateformat;1"]
-                         .getService(Ci.nsIScriptableDateFormat);
       let time = new Date(m.timeStamp);
-      time = dateServ.FormatDateTime("", dateServ.dateFormatShort,
-                                     dateServ.timeFormatSeconds,
-                                     time.getFullYear(), time.getMonth() + 1,
-                                     time.getDate(), time.getHours(),
-                                     time.getMinutes(), time.getSeconds());
+      const dateTimeFormatter = Services.intl.createDateTimeFormat(undefined, {
+        dateStyle: "short", timeStyle: "long"
+      });
+      time = dateTimeFormatter.format(time);
       let level = dbgMsg.logLevel;
       if (!level)
         return "(" + m.errorMessage + ")";
       if (level == dbgMsg.LEVEL_ERROR)
         level = "ERROR";
       else if (level == dbgMsg.LEVEL_WARNING)
         level = "WARN.";
       else if (level == dbgMsg.LEVEL_LOG)
--- a/im/content/debugLogPanel.xml
+++ b/im/content/debugLogPanel.xml
@@ -108,24 +108,21 @@
           let doc = this.browser.contentDocument;
           let table = doc.createElement("table");
           table.style = "width: 100%";
           // Clear out any existing content before appending the table.
           doc.body.innerHTML = "";
           doc.body.appendChild(table);
           for (let dbgMsg of account.getDebugMessages()) {
             let m = dbgMsg.message;
-            const dateServ = Cc["@mozilla.org/intl/scriptabledateformat;1"]
-                               .getService(Ci.nsIScriptableDateFormat);
             let time = new Date(m.timeStamp);
-            time = dateServ.FormatDateTime("", dateServ.dateFormatShort,
-                                           dateServ.timeFormatSeconds,
-                                           time.getFullYear(), time.getMonth() + 1,
-                                           time.getDate(), time.getHours(),
-                                           time.getMinutes(), time.getSeconds());
+            const dateTimeFormatter = Services.intl.createDateTimeFormat(undefined, {
+              dateStyle: "short", timeStyle: "long"
+            });
+            time = dateTimeFormatter.format(time);
             let level = dbgMsg.logLevel;
             let rowClass = "default";
             let formattedMsg;
             if (!level)
               formattedMsg = "(" + m.errorMessage + ")";
             else {
               if (level == dbgMsg.LEVEL_ERROR) {
                 level = "ERROR";
--- a/im/content/viewlog.js
+++ b/im/content/viewlog.js
@@ -231,26 +231,26 @@ chatLogTreeView.prototype = {
     if (this._tree)
       this._tree.rowCountChanged(0, -this._rowMap.length);
     this._rowMap = [];
 
     // Used to show the dates in the log list in the locale of the application.
     let chatBundle = document.getElementById("bundle_instantbird");
     let dateFormatBundle = document.getElementById("bundle_dateformat");
     let placesBundle = document.getElementById("bundle_places");
-    let dts = Cc["@mozilla.org/intl/scriptabledateformat;1"].getService(Ci.nsIScriptableDateFormat);
+    const dateFormatter = Services.intl.createDateTimeFormat(undefined,
+      { dateStyle: "short" });
+    const dateTimeFormatter = Services.intl.createDateTimeFormat(undefined, {
+      dateStyle: "short", timeStyle: "short"
+    });
     let formatDate = function(aDate) {
-      return dts.FormatDate("", dts.dateFormatShort, aDate.getFullYear(),
-                            aDate.getMonth() + 1, aDate.getDate());
+      return dateFormatter.format(aDate);
     };
     let formatDateTime = function(aDate) {
-      return dts.FormatDateTime("", dts.dateFormatShort,
-                                dts.timeFormatNoSeconds, aDate.getFullYear(),
-                                aDate.getMonth() + 1, aDate.getDate(),
-                                aDate.getHours(), aDate.getMinutes(), 0);
+      return dateTimeFormatter.format(aDate);
     };
     let formatMonthYear = function(aDate) {
       let month = formatMonth(aDate);
       return dateFormatBundle.getFormattedString("finduri-MonthYear",
                                                  [month, aDate.getFullYear()]);
     };
     let formatMonth = aDate =>
       dateFormatBundle.getString("month." + (aDate.getMonth() + 1) + ".name");
--- a/mail/components/im/content/chat-messenger-overlay.js
+++ b/mail/components/im/content/chat-messenger-overlay.js
@@ -445,40 +445,16 @@ var chatHandler = {
             this.observedContact.id == contact.id)
           return;
         this.showContactInfo(contact);
         this.observedContact = contact;
         return;
       }
     }
   },
-  _makeFriendlyDate: function(aDate) {
-    let dts = Components.classes["@mozilla.org/intl/scriptabledateformat;1"]
-                        .getService(Components.interfaces.nsIScriptableDateFormat);
-
-    // Figure out when today begins
-    let now = new Date();
-    let today = new Date(now.getFullYear(), now.getMonth(),
-                         now.getDate());
-
-    // Figure out if the end time is from today, yesterday,
-    // this week, etc.
-    let kDayInMsecs = 24 * 60 * 60 * 1000;
-    let time = dts.FormatTime("", dts.timeFormatNoSeconds,
-                              aDate.getHours(), aDate.getMinutes(),0);
-    let bundle = document.getElementById("chatBundle");
-    if (aDate >= today)
-      return bundle.getFormattedString("today", [time]);
-    if (today - aDate < kDayInMsecs)
-      return bundle.getFormattedString("yesterday", [time]);
-
-    let date = dts.FormatDate("", dts.dateFormatShort, aDate.getFullYear(),
-                              aDate.getMonth() + 1, aDate.getDate());
-    return bundle.getFormattedString("dateTime", [date, time]);
-  },
 
   /**
    * Display a list of logs into a tree, and optionally handle a default selection.
    *
    * @param aLogs An nsISimpleEnumerator of imILog.
    * @param aShouldSelect Either a boolean (true means select the first log
    * of the list, false or undefined means don't mess with the selection) or a log
    * item that needs to be selected.
@@ -1224,26 +1200,27 @@ chatLogTreeView.prototype = {
     if (this._tree)
       this._tree.rowCountChanged(0, -this._rowMap.length);
     this._rowMap = [];
 
     // Used to show the dates in the log list in the locale of the application.
     let chatBundle = document.getElementById("chatBundle");
     let dateFormatBundle = document.getElementById("bundle_dateformat");
     let placesBundle = document.getElementById("bundle_places");
-    let dts = Cc["@mozilla.org/intl/scriptabledateformat;1"].getService(Ci.nsIScriptableDateFormat);
     let formatDate = function(aDate) {
-      return dts.FormatDate("", dts.dateFormatShort, aDate.getFullYear(),
-                            aDate.getMonth() + 1, aDate.getDate());
+      const dateFormatter = Services.intl.createDateTimeFormat(undefined, {
+        dateStyle: "short"
+      });
+      return dateFormatter.format(aDate);
     };
     let formatDateTime = function(aDate) {
-      return dts.FormatDateTime("", dts.dateFormatShort,
-                                dts.timeFormatNoSeconds, aDate.getFullYear(),
-                                aDate.getMonth() + 1, aDate.getDate(),
-                                aDate.getHours(), aDate.getMinutes(), 0);
+      const dateTimeFormatter = Services.intl.createDateTimeFormat(undefined, {
+        dateStyle: "short", timeStyle: "short"
+      });
+      return dateTimeFormatter.format(aDate);
     };
     let formatMonthYear = function(aDate) {
       let month = formatMonth(aDate);
       return dateFormatBundle.getFormattedString("finduri-MonthYear",
                                                  [month, aDate.getFullYear()]);
     };
     let formatMonth = aDate =>
       dateFormatBundle.getString("month." + (aDate.getMonth() + 1) + ".name");
--- a/mail/components/im/content/imAccounts.js
+++ b/mail/components/im/content/imAccounts.js
@@ -234,24 +234,21 @@ var gAccountManager = {
     // Reconnect the account if an exception was added.
     if (params.exceptionAdded)
       account.connect();
   },
   copyDebugLog: function am_copyDebugLog() {
     let account = this.accountList.selectedItem.account;
     let text = account.getDebugMessages().map(function(dbgMsg) {
       let m = dbgMsg.message;
-      const dateServ = Cc["@mozilla.org/intl/scriptabledateformat;1"]
-                         .getService(Ci.nsIScriptableDateFormat);
       let time = new Date(m.timeStamp);
-      time = dateServ.FormatDateTime("", dateServ.dateFormatShort,
-                                     dateServ.timeFormatSeconds,
-                                     time.getFullYear(), time.getMonth() + 1,
-                                     time.getDate(), time.getHours(),
-                                     time.getMinutes(), time.getSeconds());
+      const dateTimeFormatter = Services.intl.createDateTimeFormat(undefined, {
+        dateStyle: "short", timeStyle: "long"
+      });
+      time = dateTimeFormatter.format(time);
       let level = dbgMsg.logLevel;
       if (!level)
         return "(" + m.errorMessage + ")";
       if (level == dbgMsg.LEVEL_ERROR)
         level = "ERROR";
       else if (level == dbgMsg.LEVEL_WARNING)
         level = "WARN.";
       else if (level == dbgMsg.LEVEL_LOG)
--- a/mail/locales/en-US/chrome/messenger/chat.properties
+++ b/mail/locales/en-US/chrome/messenger/chat.properties
@@ -67,25 +67,16 @@ chat.hasStoppedTyping=has stopped typing
 #  %S is replaced with the display name of the contact.
 chat.contactIsTyping=%S is typing.
 chat.contactHasStoppedTyping=%S has stopped typing.
 
 # LOCALIZATION NOTE (unknownCommand):
 # This is shown when an unknown command (/foo) is attempted. %S is the command.
 unknownCommand=%S is not a supported command. Type /help to see the list of commands.
 
-# LOCALIZATION NOTE (today, yesterday):
-# These 3 strings are used to display when previous conversations happened.
-# In 'today' and 'yesterday', %S is replaced with the time.
-today=Today %S
-yesterday=Yesterday %S
-# LOCALIZATION NOTE (dateTime):
-# %1$S is the date, %2$S is the date.
-dateTime=%1$S %2$S
-
 #LOCALIZATION NOTE
 # These are special entries in the log tree for the corresponding days.
 log.today=Today
 log.yesterday=Yesterday
 
 #LOCALIZATION NOTE
 # These are special groups in the log tree for the last 3-7 days and
 # the last 8-14 days.