commento.js: define new thresholds for time ago descriptions

This commit is contained in:
David Planella 2020-02-13 18:26:59 +01:00
parent 6978171885
commit 90f39499a1

View File

@ -809,26 +809,35 @@
function timeDifference(current, previous) { // thanks stackoverflow function timeDifference(current, previous) { // thanks stackoverflow
var msJustNow = 5000; // Times are defined in milliseconds
var msPerMinute = 60000; var msPerSecond = 1000;
var msPerHour = 3600000; var msPerMinute = 60 * msPerSecond;
var msPerDay = 86400000; var msPerHour = 60 * msPerMinute;
var msPerMonth = 2592000000; var msPerDay = 24 * msPerHour;
var msPerYear = 946080000000; var msPerMonth = 30 * msPerDay;
var msPerYear = 12 * msPerMonth;
// Time ago thresholds
var msJustNow = 5 * msPerSecond; // Up until 5 s
var msMinutesAgo = 2 * msPerMinute; // Up until 2 minutes
var msHoursAgo = 2 * msPerHour; // Up until 2 hours
var msDaysAgo = 2 * msPerDay; // Up until 2 days
var msMonthsAgo = 2 * msPerMonth; // Up until 2 months
var msYearsAgo = 2 * msPerYear; // Up until 2 years
var elapsed = current - previous; var elapsed = current - previous;
if (elapsed < msJustNow) { if (elapsed < msJustNow) {
return "just now"; return "just now";
} else if (elapsed < msPerMinute) { } else if (elapsed < msMinutesAgo) {
return Math.round(elapsed / 1000) + " seconds ago"; return Math.round(elapsed / msPerSecond) + " seconds ago";
} else if (elapsed < msPerHour) { } else if (elapsed < msHoursAgo) {
return Math.round(elapsed / msPerMinute) + " minutes ago"; return Math.round(elapsed / msPerMinute) + " minutes ago";
} else if (elapsed < msPerDay ) { } else if (elapsed < msDaysAgo ) {
return Math.round(elapsed / msPerHour ) + " hours ago"; return Math.round(elapsed / msPerHour ) + " hours ago";
} else if (elapsed < msPerMonth) { } else if (elapsed < msMonthsAgo) {
return Math.round(elapsed / msPerDay) + " days ago"; return Math.round(elapsed / msPerDay) + " days ago";
} else if (elapsed < msPerYear) { } else if (elapsed < msYearsAgo) {
return Math.round(elapsed / msPerMonth) + " months ago"; return Math.round(elapsed / msPerMonth) + " months ago";
} else { } else {
return Math.round(elapsed / msPerYear ) + " years ago"; return Math.round(elapsed / msPerYear ) + " years ago";