summaryrefslogtreecommitdiffstats
path: root/mailnews/base/util/templateUtils.js
diff options
context:
space:
mode:
authorMatt A. Tobin <email@mattatobin.com>2019-11-03 00:17:46 -0400
committerMatt A. Tobin <email@mattatobin.com>2019-11-03 00:17:46 -0400
commit302bf1b523012e11b60425d6eee1221ebc2724eb (patch)
treeb191a895f8716efcbe42f454f37597a545a6f421 /mailnews/base/util/templateUtils.js
parent21b3f6247403c06f85e1f45d219f87549862198f (diff)
downloadUXP-302bf1b523012e11b60425d6eee1221ebc2724eb.tar
UXP-302bf1b523012e11b60425d6eee1221ebc2724eb.tar.gz
UXP-302bf1b523012e11b60425d6eee1221ebc2724eb.tar.lz
UXP-302bf1b523012e11b60425d6eee1221ebc2724eb.tar.xz
UXP-302bf1b523012e11b60425d6eee1221ebc2724eb.zip
Issue #1258 - Part 1: Import mailnews, ldap, and mork from comm-esr52.9.1
Diffstat (limited to 'mailnews/base/util/templateUtils.js')
-rw-r--r--mailnews/base/util/templateUtils.js90
1 files changed, 90 insertions, 0 deletions
diff --git a/mailnews/base/util/templateUtils.js b/mailnews/base/util/templateUtils.js
new file mode 100644
index 000000000..d90f16abd
--- /dev/null
+++ b/mailnews/base/util/templateUtils.js
@@ -0,0 +1,90 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+var EXPORTED_SYMBOLS = ["PluralStringFormatter", "makeFriendlyDateAgo"];
+
+var Cc = Components.classes;
+var Ci = Components.interfaces;
+var Cr = Components.results;
+var Cu = Components.utils;
+
+Cu.import("resource://gre/modules/PluralForm.jsm");
+Cu.import("resource:///modules/StringBundle.js");
+
+function PluralStringFormatter(aBundleURI) {
+ this._bundle = new StringBundle(aBundleURI);
+}
+
+PluralStringFormatter.prototype = {
+ get: function(aStringName, aReplacements, aPluralCount) {
+ let str = this._bundle.get(aStringName);
+ if (aPluralCount !== undefined)
+ str = PluralForm.get(aPluralCount, str);
+ if (aReplacements !== undefined) {
+ for (let i = 0; i < aReplacements.length; i++)
+ str = str.replace("#" + (i+1), aReplacements[i]);
+ }
+ return str;
+ },
+};
+
+
+var gTemplateUtilsStrings = new PluralStringFormatter(
+ "chrome://messenger/locale/templateUtils.properties"
+);
+
+/**
+ * Helper function to generate a localized "friendly" representation of
+ * time relative to the present. If the time input is "today", it returns
+ * a string corresponding to just the time. If it's yesterday, it returns
+ * "yesterday" (localized). If it's in the last week, it returns the day
+ * of the week. If it's before that, it returns the date.
+ *
+ * @param time
+ * the time (better be in the past!)
+ * @return The string with a "human-friendly" representation of that time
+ * relative to now.
+ */
+function makeFriendlyDateAgo(time)
+{
+ let dts = Cc["@mozilla.org/intl/scriptabledateformat;1"]
+ .getService(Ci.nsIScriptableDateFormat);
+
+ // Figure out when today begins
+ let now = new Date();
+ let today = new Date(now.getFullYear(), now.getMonth(),
+ now.getDate());
+
+ // Get the end time to display
+ let end = time;
+
+ // Figure out if the end time is from today, yesterday,
+ // this week, etc.
+ let dateTime;
+ let kDayInMsecs = 24 * 60 * 60 * 1000;
+ let k6DaysInMsecs = 6 * kDayInMsecs;
+ if (end >= today) {
+ // activity finished after today started, show the time
+ dateTime = dts.FormatTime("", dts.timeFormatNoSeconds,
+ end.getHours(), end.getMinutes(),0);
+ } else if (today - end < kDayInMsecs) {
+ // activity finished after yesterday started, show yesterday
+ dateTime = gTemplateUtilsStrings.get("yesterday");
+ } else if (today - end < k6DaysInMsecs) {
+ // activity finished after last week started, show day of week
+ dateTime = end.toLocaleFormat("%A");
+ } else if (now.getFullYear() == end.getFullYear()) {
+ // activity must have been from some time ago.. show month/day
+ let month = end.toLocaleFormat("%B");
+ // Remove leading 0 by converting the date string to a number
+ let date = Number(end.toLocaleFormat("%d"));
+ dateTime = gTemplateUtilsStrings.get("monthDate", [month, date]);
+ } else {
+ // not this year, so show full date format
+ dateTime = dts.FormatDate("", dts.dateFormatShort,
+ end.getFullYear(), end.getMonth() + 1,
+ end.getDate());
+ }
+ return dateTime;
+}