Fix typo
[vchess.git] / client / src / utils / datetime.js
CommitLineData
6808d7a1
BA
1function zeroPad(x) {
2 return (x < 10 ? "0" : "") + x;
c794dbb8
BA
3}
4
6808d7a1
BA
5export function getDate(d) {
6 return (
7 d.getFullYear() +
8 "-" +
9 zeroPad(d.getMonth() + 1) +
10 "-" +
11 zeroPad(d.getDate())
12 );
c794dbb8
BA
13}
14
6808d7a1
BA
15export function getTime(d) {
16 return (
17 zeroPad(d.getHours()) +
18 ":" +
19 zeroPad(d.getMinutes()) +
20 ":" +
21 zeroPad(d.getSeconds())
22 );
c794dbb8 23}
5b87454c 24
6808d7a1
BA
25function padDigits(x) {
26 if (x < 10) return "0" + x;
5b87454c
BA
27 return x;
28}
29
6808d7a1 30export function ppt(t) {
5b87454c
BA
31 // "Pretty print" an amount of time given in seconds
32 const dayInSeconds = 60 * 60 * 24;
33 const hourInSeconds = 60 * 60;
ed6cfe30 34 let days = Math.floor(t / dayInSeconds);
6808d7a1
BA
35 const hours = Math.floor((t % dayInSeconds) / hourInSeconds);
36 const minutes = Math.floor((t % hourInSeconds) / 60);
5b87454c
BA
37 const seconds = Math.floor(t % 60);
38 let res = "";
1fba8875
BA
39 // NOTE: 3 days limit is rather arbitrary
40 if (days >= 3 && hours >= 12) days++;
6808d7a1 41 if (days > 0) res += days + "d ";
1fba8875 42 if (days < 3 && hours > 0) res += hours + "h ";
5b87454c 43 if (days == 0 && minutes > 0)
6808d7a1
BA
44 res += hours > 0 ? padDigits(minutes) + "m " : minutes + ":";
45 if (days == 0 && hours == 0) {
5b87454c 46 res += padDigits(seconds);
1fba8875
BA
47 // Seconds indicator, since this is the only number printed:
48 if (minutes == 0) res += "s";
050ae3b5 49 }
5b87454c
BA
50 return res.trim(); //remove potential last space
51}