Bug 1354442 - Migrate DownloadUtils.jsm to use mozIntl.DateTimeFormat. r=mak
MozReview-Commit-ID: Ev0BUOtx4C8
--- a/toolkit/mozapps/downloads/DownloadUtils.jsm
+++ b/toolkit/mozapps/downloads/DownloadUtils.jsm
@@ -37,20 +37,23 @@ this.EXPORTED_SYMBOLS = [ "DownloadUtils
* convertTimeUnits(double aSecs)
*/
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PluralForm",
"resource://gre/modules/PluralForm.jsm");
+const MS_PER_DAY = 24 * 60 * 60 * 1000;
+
this.__defineGetter__("gDecimalSymbol", function() {
delete this.gDecimalSymbol;
return this.gDecimalSymbol = Number(5.4).toLocaleString().match(/\D/);
});
var localeNumberFormatCache = new Map();
function getLocaleNumberFormat(fractionDigits) {
// Backward compatibility: don't use localized digits
@@ -337,57 +340,69 @@ this.DownloadUtils = {
* and time of invocation is used if this parameter is omitted.
* @return A pair: [compact text, complete text]
*/
getReadableDates: function DU_getReadableDates(aDate, aNow) {
if (!aNow) {
aNow = new Date();
}
- 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());
- // 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 = typeof Intl === "undefined"
- ? aDate.toLocaleFormat("%A")
- : aDate.toLocaleDateString(undefined, { weekday: "long" });
+ let dateTimeFull;
+
+ // For Android, we have to keep the non Intl API version which uses
+ // deprecated toLocaleFormat until we get Intl API.
+ //
+ // For the rest of the platform, we'll use a combination of mozIntl,
+ // Intl API and localization.
+ if (typeof Intl === "undefined") {
+ // Figure out if the time is from today, yesterday, this week, etc.
+ if (aDate >= today) {
+ dateTimeCompact = aDate.toLocaleFormat("%X");
+ } else if (today - aDate < (MS_PER_DAY)) {
+ // After yesterday started, show yesterday
+ dateTimeCompact = gBundle.GetStringFromName(gStr.yesterday);
+ } else if (today - aDate < (6 * MS_PER_DAY)) {
+ // After last week started, show day of week
+ dateTimeCompact = aDate.toLocaleFormat("%A");
+ } else {
+ // Show month/day
+ let month = aDate.toLocaleFormat("%B");
+ let date = aDate.getDate();
+ dateTimeCompact = gBundle.formatStringFromName(gStr.monthDate, [month, date], 2);
+ }
+
+ dateTimeFull = aDate.toLocaleFormat("%x %X");
} else {
- // Show month/day
- let month = typeof Intl === "undefined"
- ? aDate.toLocaleFormat("%B")
- : aDate.toLocaleDateString(undefined, { month: "long" });
- let date = aDate.getDate();
- dateTimeCompact = gBundle.formatStringFromName(gStr.monthDate, [month, date], 2);
- }
+ // Figure out if the time is from today, yesterday, this week, etc.
+ if (aDate >= today) {
+ let dts = Services.intl.createDateTimeFormat(undefined, {
+ timeStyle: "short"
+ });
+ dateTimeCompact = dts.format(aDate);
+ } else if (today - aDate < (MS_PER_DAY)) {
+ // After yesterday started, show yesterday
+ dateTimeCompact = gBundle.GetStringFromName(gStr.yesterday);
+ } else if (today - aDate < (6 * MS_PER_DAY)) {
+ // After last week started, show day of week
+ dateTimeCompact = aDate.toLocaleDateString(undefined, { weekday: "long" });
+ } else {
+ // Show month/day
+ let month = aDate.toLocaleDateString(undefined, { 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,
- aDate.getDate(),
- aDate.getHours(),
- aDate.getMinutes(),
- 0);
+ const dtOptions = { dateStyle: "long", timeStyle: "short" };
+ dateTimeFull =
+ Services.intl.createDateTimeFormat(undefined, dtOptions).format(aDate);
+ }
return [dateTimeCompact, dateTimeFull];
},
/**
* Get the appropriate display host string for a URI string depending on if
* the URI has an eTLD + 1, is an IP address, a local file, or other protocol
*
--- a/toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js
+++ b/toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js
@@ -73,23 +73,27 @@ 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);
- let dts = Components.classes["@mozilla.org/intl/scriptabledateformat;1"].
- getService(Components.interfaces.nsIScriptableDateFormat);
+ let cDtf = typeof Intl === "undefined" ? null : Services.intl.createDateTimeFormat;
- testGetReadableDates(today_11_30, dts.FormatTime("", dts.timeFormatNoSeconds,
- 11, 30, 0));
- testGetReadableDates(today_12_30, dts.FormatTime("", dts.timeFormatNoSeconds,
- 12, 30, 0));
+ testGetReadableDates(today_11_30,
+ typeof Intl === "undefined"
+ ? today_11_30.toLocaleFormat("%X")
+ : cDtf(undefined, {timeStyle: "short"}).format(today_11_30));
+ testGetReadableDates(today_12_30,
+ typeof Intl === "undefined"
+ ? today_12_30.toLocaleFormat("%X")
+ : cDtf(undefined, {timeStyle: "short"}).format(today_12_30));
+
testGetReadableDates(yesterday_11_30, "Yesterday");
testGetReadableDates(yesterday_12_30, "Yesterday");
testGetReadableDates(twodaysago,
typeof Intl === "undefined"
? twodaysago.toLocaleFormat("%A")
: twodaysago.toLocaleDateString(undefined, { weekday: "long" }));
testGetReadableDates(sixdaysago,
typeof Intl === "undefined"
@@ -97,19 +101,21 @@ function testAllGetReadableDates() {
: sixdaysago.toLocaleDateString(undefined, { weekday: "long" }));
testGetReadableDates(sevendaysago,
(typeof Intl === "undefined"
? sevendaysago.toLocaleFormat("%B")
: sevendaysago.toLocaleDateString(undefined, { 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));
+
+ const dtOptions = { dateStyle: "long", timeStyle: "short" };
+ do_check_eq(dateTimeFull, typeof Intl === "undefined"
+ ? today_11_30.toLocaleFormat("%x %X")
+ : cDtf(undefined, dtOptions).format(today_11_30));
}
function run_test() {
testConvertByteUnits(-1, "-1", "bytes");
testConvertByteUnits(1, _("1"), "bytes");
testConvertByteUnits(42, _("42"), "bytes");
testConvertByteUnits(123, _("123"), "bytes");
testConvertByteUnits(1024, _("1.0"), "KB");