Experimental multi-tabs support (TODO: prevent multi-connect)
[vchess.git] / client / src / utils / datetime.js
1 function zeroPad(x)
2 {
3 return (x<10 ? "0" : "") + x;
4 }
5
6 export function getDate(d)
7 {
8 return d.getFullYear() + '-' + zeroPad(d.getMonth()+1) + '-' + zeroPad(d.getDate());
9 }
10
11 export function getTime(d)
12 {
13 return zeroPad(d.getHours()) + ":" + zeroPad(d.getMinutes()) + ":" +
14 zeroPad(d.getSeconds());
15 }
16
17 function padDigits(x)
18 {
19 if (x < 10)
20 return "0" + x;
21 return x;
22 }
23
24 export 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) //NOTE: 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 {
42 res += padDigits(seconds);
43 if (minutes == 0)
44 res += "s"; //seconds indicator, since this is the only number printed
45 }
46 return res.trim(); //remove potential last space
47 }