From e8bfdfd9c8ec971ef12314229d4e3f2eca4c24c7 Mon Sep 17 00:00:00 2001 From: Michael Save Date: Mon, 20 Feb 2012 02:20:51 +1100 Subject: [PATCH] Automatically convert all post times to local time using Javascript. --- inc/config.php | 3 +++ templates/main.js | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/inc/config.php b/inc/config.php index 23b7af00..93e1d316 100644 --- a/inc/config.php +++ b/inc/config.php @@ -460,6 +460,9 @@ // http://www.php.net/manual/en/function.strftime.php $config['post_date'] = '%m/%d/%y (%a) %H:%M:%S'; + // Automatically convert all post times to users' local time using Javascript. + $config['javascript_local_time'] = false; + // Same as above, but used for "you are banned' pages. $config['ban_date'] = '%A %e %B, %Y'; diff --git a/templates/main.js b/templates/main.js index d8d98990..4737e4b1 100644 --- a/templates/main.js +++ b/templates/main.js @@ -168,8 +168,42 @@ function init_expanding() { } } -function init() -{ +{% endraw %} +{% if config.javascript_local_time %} +{% raw %} +function init_localtime(){ + var iso8601 = function iso8601(s) { + s = s.replace(/\.\d\d\d+/,""); // remove milliseconds + s = s.replace(/-/,"/").replace(/-/,"/"); + s = s.replace(/T/," ").replace(/Z/," UTC"); + s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400 + return new Date(s); + }; + var zeropad = function(num, count) { + return [Math.pow(10, count - num.toString().length), num].join('').substr(1); + }; + + var times = document.getElementsByTagName('time'); + for (var i = 0; i < times.length ; i++) { + if(!times[i].textContent.match(/^\d+\/\d+\/\d+ \(\w+\) \d+:\d+:\d+$/)) { + continue; + } + + var t = iso8601(times[i].getAttribute('datetime')); + + times[i].textContent = + // date + zeropad(t.getMonth(), 2) + "/" + zeropad(t.getDate(), 2) + "/" + t.getFullYear().toString().substring(2) + + " (" + ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][t.getDay()] + ") " + + // time + zeropad(t.getHours(), 2) + ":" + zeropad(t.getMinutes(), 2) + ":" + zeropad(t.getSeconds(), 2); + } +}; +{% endraw %} +{% endif %} +{% raw %} + +function init() { newElement = document.createElement('div'); newElement.className = 'styles'; @@ -192,6 +226,7 @@ function init() highlightReply(window.location.hash.substring(1)); {% endraw %}{% if config.inline_expanding %}{% raw %}init_expanding();{% endraw %}{% endif %}{% raw %} + {% endraw %}{% if config.javascript_local_time %}{% raw %}init_localtime();{% endraw %}{% endif %}{% raw %} } var RecaptchaOptions = {