X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=client%2Fsrc%2Futils%2Fdatetime.js;h=00e8b4b9f4dfb7e55552ea418f6c95bf6c196d4d;hp=3075bffc91aac9151f65e6ba18bf8e6f879fea23;hb=5b87454c515b1fbf94e2c07ba7cb0cff21f38620;hpb=967a2686ea801d4b33129d78087651451ef1904b diff --git a/client/src/utils/datetime.js b/client/src/utils/datetime.js index 3075bffc..00e8b4b9 100644 --- a/client/src/utils/datetime.js +++ b/client/src/utils/datetime.js @@ -13,3 +13,31 @@ export function getTime(d) return zeroPad(d.getHours()) + ":" + zeroPad(d.getMinutes()) + ":" + zeroPad(d.getSeconds()); } + +function padDigits(x) +{ + if (x < 10) + return "0" + x; + return x; +} + +export function ppt(t) +{ + // "Pretty print" an amount of time given in seconds + const dayInSeconds = 60 * 60 * 24; + const hourInSeconds = 60 * 60; + const days = Math.floor(t / dayInSeconds); + const hours = Math.floor(t%dayInSeconds / hourInSeconds); + const minutes = Math.floor(t%hourInSeconds / 60); + const seconds = Math.floor(t % 60); + let res = ""; + if (days > 0) + res += days + "d "; + if (days <= 3 && hours > 0) //TODO: 3 is arbitrary + res += hours + "h "; + if (days == 0 && minutes > 0) + res += (hours > 0 ? padDigits(minutes) + "m " : minutes + ":"); + if (days == 0 && hours == 0) + res += padDigits(seconds); + return res.trim(); //remove potential last space +}