2012-03-15 06:19:26 +01:00
|
|
|
/*
|
|
|
|
* local-time.js
|
2012-03-31 02:13:11 +02:00
|
|
|
* https://github.com/savetheinternet/Tinyboard/blob/master/js/local-time.js
|
2012-03-15 06:19:26 +01:00
|
|
|
*
|
|
|
|
* Released under the MIT license
|
|
|
|
* Copyright (c) 2012 Michael Save <savetheinternet@tinyboard.org>
|
2014-01-19 14:27:24 +01:00
|
|
|
* Copyright (c) 2013-2014 Marcin Łabanowski <marcin@6irc.net>
|
2012-03-15 06:19:26 +01:00
|
|
|
*
|
|
|
|
* Usage:
|
2013-07-31 10:37:07 +02:00
|
|
|
* // $config['additional_javascript'][] = 'js/jquery.min.js';
|
2014-02-23 17:58:16 +01:00
|
|
|
* // $config['additional_javascript'][] = 'js/strftime.min.js';
|
2012-03-15 06:19:26 +01:00
|
|
|
* $config['additional_javascript'][] = 'js/local-time.js';
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-01-22 17:03:37 +01:00
|
|
|
$(document).ready(function(){
|
2015-01-01 07:10:26 +01:00
|
|
|
'use strict';
|
|
|
|
|
2012-03-15 06:19:26 +01:00
|
|
|
var iso8601 = function(s) {
|
2024-06-06 09:02:06 +02:00
|
|
|
var parts = s.split('T');
|
|
|
|
if (parts.length === 2) {
|
|
|
|
var timeParts = parts[1].split(':');
|
|
|
|
if (timeParts.length === 3) {
|
2024-06-21 05:38:35 +02:00
|
|
|
var seconds = timeParts[2];
|
|
|
|
if (seconds.length > 2) {
|
|
|
|
seconds = seconds.substr(0, 2) + '.' + seconds.substr(2);
|
2024-06-06 09:02:06 +02:00
|
|
|
}
|
2024-06-21 05:38:35 +02:00
|
|
|
// Ensure seconds part is valid
|
|
|
|
if (parseFloat(seconds) > 59) {
|
|
|
|
seconds = '59';
|
|
|
|
}
|
|
|
|
timeParts[2] = seconds;
|
2024-06-06 09:02:06 +02:00
|
|
|
}
|
|
|
|
parts[1] = timeParts.join(':');
|
|
|
|
}
|
|
|
|
s = parts.join('T');
|
2024-06-21 05:38:35 +02:00
|
|
|
|
2024-06-06 09:02:06 +02:00
|
|
|
if (!s.endsWith('Z')) {
|
|
|
|
s += 'Z';
|
|
|
|
}
|
2012-03-15 06:19:26 +01:00
|
|
|
return new Date(s);
|
|
|
|
};
|
2024-06-06 09:02:06 +02:00
|
|
|
|
2012-03-15 06:19:26 +01:00
|
|
|
var zeropad = function(num, count) {
|
|
|
|
return [Math.pow(10, count - num.toString().length), num].join('').substr(1);
|
|
|
|
};
|
2013-07-03 03:09:05 +02:00
|
|
|
|
2014-02-23 17:58:16 +01:00
|
|
|
var dateformat = (typeof strftime === 'undefined') ? function(t) {
|
|
|
|
return zeropad(t.getMonth() + 1, 2) + "/" + zeropad(t.getDate(), 2) + "/" + t.getFullYear().toString().substring(2) +
|
2024-06-06 09:02:06 +02:00
|
|
|
" (" + ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][t.getDay()] + ") " +
|
2014-02-23 17:58:16 +01:00
|
|
|
// time
|
|
|
|
zeropad(t.getHours(), 2) + ":" + zeropad(t.getMinutes(), 2) + ":" + zeropad(t.getSeconds(), 2);
|
|
|
|
} : function(t) {
|
|
|
|
// post_date is defined in templates/main.js
|
|
|
|
return strftime(window.post_date, t, datelocale);
|
|
|
|
};
|
|
|
|
|
2015-01-01 07:10:26 +01:00
|
|
|
function timeDifference(current, previous) {
|
|
|
|
var msPerMinute = 60 * 1000;
|
|
|
|
var msPerHour = msPerMinute * 60;
|
|
|
|
var msPerDay = msPerHour * 24;
|
|
|
|
var msPerMonth = msPerDay * 30;
|
|
|
|
var msPerYear = msPerDay * 365;
|
|
|
|
|
|
|
|
var elapsed = current - previous;
|
|
|
|
|
|
|
|
if (elapsed < msPerMinute) {
|
|
|
|
return 'Just now';
|
|
|
|
} else if (elapsed < msPerHour) {
|
2024-06-06 09:02:06 +02:00
|
|
|
return Math.round(elapsed / msPerMinute) + (Math.round(elapsed / msPerMinute) <= 1 ? ' minute ago' : ' minutes ago');
|
|
|
|
} else if (elapsed < msPerDay) {
|
|
|
|
return Math.round(elapsed / msPerHour) + (Math.round(elapsed / msPerHour) <= 1 ? ' hour ago' : ' hours ago');
|
2015-01-01 07:10:26 +01:00
|
|
|
} else if (elapsed < msPerMonth) {
|
2024-06-06 09:02:06 +02:00
|
|
|
return Math.round(elapsed / msPerDay) + (Math.round(elapsed / msPerDay) <= 1 ? ' day ago' : ' days ago');
|
2015-01-01 07:10:26 +01:00
|
|
|
} else if (elapsed < msPerYear) {
|
2024-06-06 09:02:06 +02:00
|
|
|
return Math.round(elapsed / msPerMonth) + (Math.round(elapsed / msPerMonth) <= 1 ? ' month ago' : ' months ago');
|
2015-01-01 07:10:26 +01:00
|
|
|
} else {
|
2024-06-06 09:02:06 +02:00
|
|
|
return Math.round(elapsed / msPerYear) + (Math.round(elapsed / msPerYear) <= 1 ? ' year ago' : ' years ago');
|
2015-01-01 07:10:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-06 09:02:06 +02:00
|
|
|
var do_localtime = function(elem) {
|
2013-07-03 03:09:05 +02:00
|
|
|
var times = elem.getElementsByTagName('time');
|
2015-01-01 07:10:26 +01:00
|
|
|
var currentTime = Date.now();
|
|
|
|
|
2024-06-06 09:02:06 +02:00
|
|
|
for (var i = 0; i < times.length; i++) {
|
2015-01-01 07:10:26 +01:00
|
|
|
var t = times[i].getAttribute('datetime');
|
2024-06-06 09:02:06 +02:00
|
|
|
var postTime = iso8601(t);
|
2015-01-01 07:10:26 +01:00
|
|
|
|
2013-07-03 03:09:05 +02:00
|
|
|
times[i].setAttribute('data-local', 'true');
|
2015-01-01 07:10:26 +01:00
|
|
|
|
2016-10-09 02:11:55 +02:00
|
|
|
if (localStorage.show_relative_time === 'false') {
|
2024-06-06 09:02:06 +02:00
|
|
|
times[i].innerHTML = dateformat(postTime);
|
2015-01-01 07:10:26 +01:00
|
|
|
times[i].setAttribute('title', timeDifference(currentTime, postTime.getTime()));
|
|
|
|
} else {
|
|
|
|
times[i].innerHTML = timeDifference(currentTime, postTime.getTime());
|
2024-06-06 09:02:06 +02:00
|
|
|
times[i].setAttribute('title', dateformat(postTime));
|
2015-01-01 07:10:26 +01:00
|
|
|
}
|
|
|
|
}
|
2012-03-31 10:12:08 +02:00
|
|
|
};
|
2013-07-03 03:09:05 +02:00
|
|
|
|
2015-01-01 07:10:26 +01:00
|
|
|
if (window.Options && Options.get_tab('general') && window.jQuery) {
|
|
|
|
var interval_id;
|
|
|
|
Options.extend_tab('general', '<label id="show-relative-time"><input type="checkbox">' + _('Show relative time') + '</label>');
|
|
|
|
|
|
|
|
$('#show-relative-time>input').on('change', function() {
|
2016-10-09 02:02:13 +02:00
|
|
|
if (localStorage.show_relative_time !== 'false') {
|
2015-01-01 07:10:26 +01:00
|
|
|
localStorage.show_relative_time = 'false';
|
|
|
|
clearInterval(interval_id);
|
|
|
|
} else {
|
|
|
|
localStorage.show_relative_time = 'true';
|
|
|
|
interval_id = setInterval(do_localtime, 30000, document);
|
|
|
|
}
|
|
|
|
// no need to refresh page
|
|
|
|
do_localtime(document);
|
|
|
|
});
|
|
|
|
|
2016-10-09 02:02:13 +02:00
|
|
|
if (localStorage.show_relative_time !== 'false') {
|
2024-06-06 09:02:06 +02:00
|
|
|
$('#show-relative-time>input').attr('checked', 'checked');
|
2015-01-01 07:10:26 +01:00
|
|
|
interval_id = setInterval(do_localtime, 30000, document);
|
|
|
|
}
|
|
|
|
|
2013-07-31 10:37:07 +02:00
|
|
|
// allow to work with auto-reload.js, etc.
|
2014-01-21 19:25:11 +01:00
|
|
|
$(document).on('new_post', function(e, post) {
|
2013-07-31 10:37:07 +02:00
|
|
|
do_localtime(post);
|
|
|
|
});
|
|
|
|
}
|
2012-03-15 06:19:26 +01:00
|
|
|
|
2015-01-01 07:10:26 +01:00
|
|
|
do_localtime(document);
|
|
|
|
});
|
2024-06-21 05:38:35 +02:00
|
|
|
|