Update on game.clocks. Unfinished
[vchess.git] / client / src / utils / datetime.js
... / ...
CommitLineData
1function zeroPad(x)
2{
3 return (x<10 ? "0" : "") + x;
4}
5
6export function getDate(d)
7{
8 return d.getFullYear() + '-' + zeroPad(d.getMonth()+1) + '-' + zeroPad(d.getDate());
9}
10
11export function getTime(d)
12{
13 return zeroPad(d.getHours()) + ":" + zeroPad(d.getMinutes()) + ":" +
14 zeroPad(d.getSeconds());
15}
16
17function padDigits(x)
18{
19 if (x < 10)
20 return "0" + x;
21 return x;
22}
23
24export function ppt(t)
25{
26 // "Pretty print" an amount of time given in seconds
27 const dayInSeconds = 60 * 60 * 24;
28 const hourInSeconds = 60 * 60;
29 const days = Math.floor(t / dayInSeconds);
30 const hours = Math.floor(t%dayInSeconds / hourInSeconds);
31 const minutes = Math.floor(t%hourInSeconds / 60);
32 const seconds = Math.floor(t % 60);
33 let res = "";
34 if (days > 0)
35 res += days + "d ";
36 if (days <= 3 && hours > 0) //TODO: 3 is arbitrary
37 res += hours + "h ";
38 if (days == 0 && minutes > 0)
39 res += (hours > 0 ? padDigits(minutes) + "m " : minutes + ":");
40 if (days == 0 && hours == 0)
41 res += padDigits(seconds);
42 return res.trim(); //remove potential last space
43}