nekrochan/static/js/time.js

126 řádky
3.2 KiB
JavaScript
Surový Normální zobrazení Historie

const MINUTE = 60000,
HOUR = 3600000,
DAY = 86400000,
WEEK = 604800000,
MONTH = 2592000000,
YEAR = 31536000000;
2024-03-02 23:19:48 +00:00
$(function () {
$(window).on("setup_post_events", function (event) {
setup_events($(`#${event.id}`).find("time"));
});
setup_events($("time"));
setInterval(() => {
setup_events($("time"));
}, 60000);
function setup_events(elements) {
elements.each(function () {
let title = $(this).attr("title");
2024-03-02 23:19:48 +00:00
if (!title) {
$(this).attr("title", $(this).text());
}
let rel = reltime($(this).attr("datetime"));
$(this).text(rel);
});
}
2024-03-02 23:19:48 +00:00
function reltime(date) {
let delta = Date.now() - Date.parse(date);
let fut = false;
2024-03-02 23:19:48 +00:00
if (delta < 0) {
delta = Math.abs(delta);
fut = true;
}
2024-03-02 23:19:48 +00:00
let minutes = Math.floor(delta / MINUTE);
let hours = Math.floor(delta / HOUR);
let days = Math.floor(delta / DAY);
let weeks = Math.floor(delta / WEEK);
let months = Math.floor(delta / MONTH);
let years = Math.floor(delta / YEAR);
let rt = "Teď";
if (minutes > 0) {
if (fut) {
rt = `za ${minutes} ${plural("minutu|minuty|minut", minutes)}`;
} else {
rt = `před ${minutes} ${plural(
"minutou|minutami|minutami",
minutes
)}`;
}
}
2024-03-02 23:19:48 +00:00
if (hours > 0) {
if (fut) {
rt = `za ${hours} ${plural("hodinu|hodiny|hodin", hours)}`;
} else {
rt = `před ${hours} ${plural(
"hodinou|hodinami|hodinami",
hours
)}`;
}
}
2024-03-02 23:19:48 +00:00
if (days > 0) {
if (fut) {
rt = `za ${days} ${plural("den|dny|dnů", days)}`;
} else {
rt = `před ${days} ${plural("dnem|dny|dny", days)}`;
}
}
2024-03-02 23:19:48 +00:00
if (weeks > 0) {
if (fut) {
rt = `za ${weeks} ${plural("týden|týdny", weeks)}`;
} else {
rt = `před ${weeks} ${plural("týdnem|týdny", weeks)}`;
}
}
2024-03-02 23:19:48 +00:00
if (months > 0) {
if (fut) {
rt = `za ${months} ${plural("měsíc|měsíce|měsíců", months)}`;
} else {
rt = `před ${months} ${plural(
"měsícem|měsíci|měsíci",
months
)}`;
}
}
2024-03-02 23:19:48 +00:00
if (years > 0) {
if (fut) {
rt = `za ${years} ${plural("rok|roky|let", years)}`;
} else {
rt = `před ${years} ${plural("rokem|lety|lety", years)}`;
}
}
2024-03-02 23:19:48 +00:00
return rt;
}
2024-03-02 23:19:48 +00:00
function plural(plurals, count) {
let plurals_arr = plurals.split("|");
let one = plurals_arr[0];
let few = plurals_arr[1];
let other = plurals_arr[2];
if (count === 1) {
return one;
} else if (count < 5 && count !== 0) {
return few;
} else {
return other;
}
}
2024-03-02 23:19:48 +00:00
});