SpreadQuiz/js/common.js
Epagris 59e113333a - human readable to unix timestamp conversion in clion code added
- test-related request block redesigned
- elvis operator replaced with the more adequate null coalescing one
2024-09-12 14:35:36 +02:00

52 lines
1.4 KiB
JavaScript

function unix_time_to_human_readable(tunix) {
const date = new Date(Number(tunix) * 1000);
return date.getFullYear() + "-" + String(date.getMonth() + 1).padStart(2, "0") + "-" + String(date.getDate()).padStart(2, "0") + " " +
String(date.getHours()).padStart(2, "0") + ":" + String(date.getMinutes()).padStart(2, "0") + ":" + String(date.getSeconds()).padStart(2, "0");
}
function human_readable_to_unix_time(hrtime) {
return (new Date(hrtime)).getTime() / 1000;
}
function seconds_to_time(s) {
let hours = Math.floor(s / 3600);
s -= hours * 3600;
let minutes = Math.floor(s / 60);
s -= minutes * 60;
let seconds = s;
return String(hours).padStart(2, "0") + ":"
+ String(minutes).padStart(2, "0") + ":"
+ String(seconds).padStart(2, "0");
}
function time_to_seconds(t) {
let s = 0;
let parts = t.split(":").reverse();
if (parts.length >= 1) {
s += Number(parts[0]);
if (parts.length >= 2) {
s += Number(parts[1]) * 60;
if (parts.length >= 3) {
s += Number(parts[2]) * 3600;
}
}
}
return s;
}
function preprocess_inserts(str) {
let code_delim = '`';
let parts = str.split(code_delim);
let res = "";
for (let i = 0; i < parts.length; i++) {
res += parts[i];
if (i % 2 === 0) {
res += "<code>";
} else {
res += "</code>";
}
}
return res;
}