aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/RealTimer.java
blob: b4563c09b662abda658f77992743557febc6be10 (plain) (blame)
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
52
53
54
55
56
57
58
59
60
61
62
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core;

import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Implementation of timer used when running for real.
 */
public class RealTimer implements Timer {

    public long getCurrentTimeInMillis() {
        return System.currentTimeMillis();
    }

    public static String printDuration(long time) {
        StringBuilder sb = new StringBuilder();
        if (time > 1000 * 60 * 60 * 24 * 2) {
            double days = time / (1000.0 * 60 * 60 * 24);
            sb.append(String.format(Locale.ENGLISH, "%.2f", days)).append(" day").append(Math.abs(days - 1.0) < 0.0001 ? "" : "s");
        } else if (time > 1000 * 60 * 60 * 2) {
            double hours = time / (1000.0 * 60 * 60);
            sb.append(String.format(Locale.ENGLISH, "%.2f", hours)).append(" hour").append(Math.abs(hours - 1.0) < 0.0001 ? "" : "s");
        } else if (time > 1000 * 60 * 2) {
            double minutes = time / (1000.0 * 60);
            sb.append(String.format(Locale.ENGLISH, "%.2f", minutes)).append(" minute").append(Math.abs(minutes - 1.0) < 0.0001 ? "" : "s");
        } else if (time > 1000 * 2) {
            double seconds = time / (1000.0);
            sb.append(String.format(Locale.ENGLISH, "%.2f", seconds)).append(" s");
        } else {
            sb.append(time).append(" ms");
        }
        return sb.toString();
    }

    public static String printDateNoMilliSeconds(long time, TimeZone tz) {
        Calendar cal = Calendar.getInstance(tz);
        cal.setTimeInMillis(time);
        return String.format(Locale.ENGLISH, "%04d-%02d-%02d %02d:%02d:%02d",
                cal.get(Calendar.YEAR),
                cal.get(Calendar.MONTH) + 1,
                cal.get(Calendar.DAY_OF_MONTH),
                cal.get(Calendar.HOUR_OF_DAY),
                cal.get(Calendar.MINUTE),
                cal.get(Calendar.SECOND));
    }

    public static String printDate(long time, TimeZone tz) {
        Calendar cal = Calendar.getInstance(tz);
        cal.setTimeInMillis(time);
        return String.format(Locale.ENGLISH, "%04d-%02d-%02d %02d:%02d:%02d.%03d",
                             cal.get(Calendar.YEAR),
                             cal.get(Calendar.MONTH) + 1,
                             cal.get(Calendar.DAY_OF_MONTH),
                             cal.get(Calendar.HOUR_OF_DAY),
                             cal.get(Calendar.MINUTE),
                             cal.get(Calendar.SECOND),
                             cal.get(Calendar.MILLISECOND));
    }

}