Bug 1313796 - Remove Date.prototype.toLocaleFormat uses in toolkits/mozapps/downloads. r=mak
authorAndré Bargull <andre.bargull@gmail.com>
Mon, 07 Nov 2016 08:36:36 -0800
changeset 351491 a19dfd28f8b224e1854e26f06b09263523b39b97
parent 351490 a19077a2bd28018d94e7b4af5b60d6180c9ec42f
child 351492 e2acfe066887bb24724f0ee3ba3a532e2491a5ff
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs1313796
milestone52.0a1
Bug 1313796 - Remove Date.prototype.toLocaleFormat uses in toolkits/mozapps/downloads. r=mak
toolkit/mozapps/downloads/DownloadUtils.jsm
toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js
--- a/toolkit/mozapps/downloads/DownloadUtils.jsm
+++ b/toolkit/mozapps/downloads/DownloadUtils.jsm
@@ -350,36 +350,47 @@ this.DownloadUtils = {
     }
 
     let dts = Cc["@mozilla.org/intl/scriptabledateformat;1"]
               .getService(Ci.nsIScriptableDateFormat);
 
     // Figure out when today begins
     let today = new Date(aNow.getFullYear(), aNow.getMonth(), aNow.getDate());
 
+    // Get locale to use for date/time formatting
+    // TODO: Remove Intl fallback when bug 1215247 is fixed.
+    const locale = typeof Intl === "undefined"
+                   ? undefined
+                   : Cc["@mozilla.org/chrome/chrome-registry;1"]
+                       .getService(Ci.nsIXULChromeRegistry)
+                       .getSelectedLocale("global", true);
+
     // Figure out if the time is from today, yesterday, this week, etc.
     let dateTimeCompact;
     if (aDate >= today) {
       // After today started, show the time
       dateTimeCompact = dts.FormatTime("",
                                        dts.timeFormatNoSeconds,
                                        aDate.getHours(),
                                        aDate.getMinutes(),
                                        0);
     } else if (today - aDate < (24 * 60 * 60 * 1000)) {
       // After yesterday started, show yesterday
       dateTimeCompact = gBundle.GetStringFromName(gStr.yesterday);
     } else if (today - aDate < (6 * 24 * 60 * 60 * 1000)) {
       // After last week started, show day of week
-      dateTimeCompact = aDate.toLocaleFormat("%A");
+      dateTimeCompact = typeof Intl === "undefined"
+                        ? aDate.toLocaleFormat("%A")
+                        : aDate.toLocaleDateString(locale, { weekday: "long" });
     } else {
       // Show month/day
-      let month = aDate.toLocaleFormat("%B");
-      // Remove leading 0 by converting the date string to a number
-      let date = Number(aDate.toLocaleFormat("%d"));
+      let month = typeof Intl === "undefined"
+                  ? aDate.toLocaleFormat("%B")
+                  : aDate.toLocaleDateString(locale, { month: "long" });
+      let date = aDate.getDate();
       dateTimeCompact = gBundle.formatStringFromName(gStr.monthDate, [month, date], 2);
     }
 
     let dateTimeFull = dts.FormatDateTime("",
                                           dts.dateFormatLong,
                                           dts.timeFormatNoSeconds,
                                           aDate.getFullYear(),
                                           aDate.getMonth() + 1,
--- a/toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js
+++ b/toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js
@@ -79,29 +79,45 @@ function testAllGetReadableDates()
   const today_11_30     = new Date(2000, 11, 31, 11, 30, 15);
   const today_12_30     = new Date(2000, 11, 31, 12, 30, 15);
   const yesterday_11_30 = new Date(2000, 11, 30, 11, 30, 15);
   const yesterday_12_30 = new Date(2000, 11, 30, 12, 30, 15);
   const twodaysago      = new Date(2000, 11, 29, 11, 30, 15);
   const sixdaysago      = new Date(2000, 11, 25, 11, 30, 15);
   const sevendaysago    = new Date(2000, 11, 24, 11, 30, 15);
 
+  // TODO: Remove Intl fallback when bug 1215247 is fixed.
+  const locale = typeof Intl === "undefined"
+                 ? undefined
+                 : Components.classes["@mozilla.org/chrome/chrome-registry;1"]
+                                     .getService(Components.interfaces.nsIXULChromeRegistry)
+                                     .getSelectedLocale("global", true);
+
   let dts = Components.classes["@mozilla.org/intl/scriptabledateformat;1"].
             getService(Components.interfaces.nsIScriptableDateFormat);
 
   testGetReadableDates(today_11_30, dts.FormatTime("", dts.timeFormatNoSeconds,
                                                    11, 30, 0));
   testGetReadableDates(today_12_30, dts.FormatTime("", dts.timeFormatNoSeconds,
                                                    12, 30, 0));
   testGetReadableDates(yesterday_11_30, "Yesterday");
   testGetReadableDates(yesterday_12_30, "Yesterday");
-  testGetReadableDates(twodaysago, twodaysago.toLocaleFormat("%A"));
-  testGetReadableDates(sixdaysago, sixdaysago.toLocaleFormat("%A"));
-  testGetReadableDates(sevendaysago, sevendaysago.toLocaleFormat("%B") + " " +
-                                     sevendaysago.toLocaleFormat("%d"));
+  testGetReadableDates(twodaysago,
+                       typeof Intl === "undefined"
+                       ? twodaysago.toLocaleFormat("%A")
+                       : twodaysago.toLocaleDateString(locale, { weekday: "long" }));
+  testGetReadableDates(sixdaysago,
+                       typeof Intl === "undefined"
+                       ? sixdaysago.toLocaleFormat("%A")
+                       : sixdaysago.toLocaleDateString(locale, { weekday: "long" }));
+  testGetReadableDates(sevendaysago,
+                       (typeof Intl === "undefined"
+                        ? sevendaysago.toLocaleFormat("%B")
+                        : sevendaysago.toLocaleDateString(locale, { month: "long" })) + " " +
+                       sevendaysago.getDate().toString().padStart(2, "0"));
 
   let [, dateTimeFull] = DownloadUtils.getReadableDates(today_11_30);
   do_check_eq(dateTimeFull, dts.FormatDateTime("", dts.dateFormatLong,
                                                    dts.timeFormatNoSeconds,
                                                    2000, 12, 31, 11, 30, 0));
 }
 
 function run_test()