aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/world/anhgelus/molehunt/utils/TimeUtils.java
blob: 2b96606e1784f31884aad2b57a48f0f233f8ecb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package world.anhgelus.molehunt.utils;

public class TimeUtils {

    private record Time(long hours, long minutes, long seconds) {}

    public static String generateString(long time) {
        final var pt = generateTime(time);

        StringBuilder sb = new StringBuilder();
        if (pt.hours != 0) {
            sb.append(pt.hours).append(" hours ");
        }
        if (pt.minutes != 0 || pt.hours != 0) {
            sb.append(pt.minutes).append(" minutes ");
        }
        sb.append(pt.seconds).append(" seconds");

        return sb.toString();
    }

    public static String generateShortString(long time) {
        final var pt = generateTime(time);

        return padLeft(pt.hours) + ":" +
                padLeft(pt.minutes) + ":" +
                padLeft(pt.seconds);
    }

    private static Time generateTime(long time) {
        long hours = 0;
        if (time > 3600) {
            hours = Math.floorDiv(time, 3600);
        }
        long minutes = 0;
        if (hours != 0 || time > 60) {
            minutes = Math.floorDiv(time - hours * 3600, 60);
        }
        long seconds = (long) Math.floor(time - hours * 3600 - minutes * 60);
        return new Time(hours, minutes, seconds);
    }

    private static String padLeft(long n) {
        if (n < 10 && n != 0) {
            return "0" + Math.round(n);
        } else if (n == 0) {
            return "00";
        }
        return Long.toString(Math.round(n));
    }
}