summaryrefslogtreecommitdiff
path: root/underlays/javascript
diff options
context:
space:
mode:
authorJoey Hess <joey@kodama.kitenet.net>2008-10-17 20:47:32 -0400
committerJoey Hess <joey@kodama.kitenet.net>2008-10-17 20:47:32 -0400
commit7390a7a0721d9985c88efec1fd0e3a19d0368c82 (patch)
tree5573d9a86849bb3d180ad26132e8f1abf6fdd53c /underlays/javascript
parent9a62836d288fbced42b04a172baf52063ef71c61 (diff)
relativedate: New javascript-alicious plugin that makes all dates display relative, in a very nice way, if I say so myself.
Diffstat (limited to 'underlays/javascript')
-rw-r--r--underlays/javascript/relativedate.js70
1 files changed, 70 insertions, 0 deletions
diff --git a/underlays/javascript/relativedate.js b/underlays/javascript/relativedate.js
new file mode 100644
index 000000000..c5e62b3cc
--- /dev/null
+++ b/underlays/javascript/relativedate.js
@@ -0,0 +1,70 @@
+// Causes html elements in the 'date' and 'pagedate' classes to be displayed
+// as relative dates. The date is parsed from the title attribute, or from
+// the element content.
+
+var dateElements;
+
+hook("onload", getDates);
+
+function getDates() {
+ dateElements = getElementsByClass('date');
+ for (var i = 0; i < dateElements.length; i++) {
+ var elt = dateElements[i];
+ var title = elt.attributes.title;
+ var d = new Date(title ? title.value : elt.innerHTML);
+ if (! isNaN(d)) {
+ dateElements[i].date=d;
+ elt.title=elt.innerHTML;
+ }
+ }
+
+ showDates();
+}
+
+function showDates() {
+ for (var i = 0; i < dateElements.length; i++) {
+ var elt = dateElements[i];
+ var d = elt.date;
+ if (! isNaN(d)) {
+ elt.innerHTML=relativeDate(d);
+ }
+ }
+ setTimeout(showDates,30000); // keep updating every 30s
+}
+
+var timeUnits = new Array;
+timeUnits['minute'] = 60;
+timeUnits['hour'] = timeUnits['minute'] * 60;
+timeUnits['day'] = timeUnits['hour'] * 24;
+timeUnits['month'] = timeUnits['day'] * 30;
+timeUnits['year'] = timeUnits['day'] * 364;
+var timeUnitOrder = ['year', 'month', 'day', 'hour', 'minute'];
+
+function relativeDate(date) {
+ var now = new Date();
+ var offset = date.getTime() - now.getTime();
+ var seconds = Math.round(Math.abs(offset) / 1000);
+
+ var ret = "";
+ var shown = 0;
+ for (i = 0; i < timeUnitOrder.length; i++) {
+ var unit = timeUnitOrder[i];
+ if (seconds >= timeUnits[unit]) {
+ var num = Math.floor(seconds / timeUnits[unit]);
+ seconds -= num * timeUnits[unit];
+ if (ret)
+ ret += "and ";
+ ret += num + " " + unit + (num > 1 ? "s" : "") + " ";
+
+ if (++shown == 2)
+ break;
+ }
+ else if (shown)
+ break;
+ }
+
+ if (! ret)
+ ret = "less than a minute "
+
+ return ret + (offset < 0 ? "ago" : "from now");
+}