Convert all remaining tabs by 2spaces
[vchess.git] / client / src / utils / datetime.js
CommitLineData
c794dbb8
BA
1function zeroPad(x)
2{
dac39588 3 return (x<10 ? "0" : "") + x;
c794dbb8
BA
4}
5
8d61fc4a 6export function getDate(d)
c794dbb8 7{
dac39588 8 return d.getFullYear() + '-' + zeroPad(d.getMonth()+1) + '-' + zeroPad(d.getDate());
c794dbb8
BA
9}
10
8d61fc4a 11export function getTime(d)
c794dbb8 12{
dac39588
BA
13 return zeroPad(d.getHours()) + ":" + zeroPad(d.getMinutes()) + ":" +
14 zeroPad(d.getSeconds());
c794dbb8 15}
5b87454c
BA
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 ";
050ae3b5 36 if (days <= 3 && hours > 0) //NOTE: 3 is arbitrary
5b87454c
BA
37 res += hours + "h ";
38 if (days == 0 && minutes > 0)
39 res += (hours > 0 ? padDigits(minutes) + "m " : minutes + ":");
40 if (days == 0 && hours == 0)
050ae3b5 41 {
5b87454c 42 res += padDigits(seconds);
050ae3b5
BA
43 if (minutes == 0)
44 res += "s"; //seconds indicator, since this is the only number printed
45 }
5b87454c
BA
46 return res.trim(); //remove potential last space
47}