Question

I am trying to out the groovy time duration without the default field names, i.e. days, hours, minutes, seconds, etc.

This formatted time difference is being put at the end of a progress bar so the real time run time of the process is displayed.

public static String calculateTime(Date timeStart)
{
        Date timeStop = new Date()
        TimeDuration duration = TimeCategory.minus(timeStop, timeStart)
        return "${duration.days}:${duration.hours}:${duration.minutes}:${duration.seconds}".toString()
        //return duration.toString()
}


public static void printProgBar(long num, long den, Date startDate){
    def bar = "[";
        DecimalFormat df = new DecimalFormat("#.00");
        def dataType = "";

        def len = den.toString().length()

        if(den == 0)
        {
                den = 1;
        }

        BigDecimal percent = ((num/den)*100)

        if(len <= 3){ dataType = "bytes" }
        if(len <= 6 && len > 3){ dataType = "Kbs" }
        if(len <= 9 && len > 6){ dataType = "Mbs" }
        if(len <= 12 && len > 9){ dataType = "Gbs" }

        def n = num
        def d = den
        if(dataType.equals("Kbs"))
        {
            n = num/1024
            d = den/1024
        }
        if(dataType.equals("Mbs"))
        {
            n = num/1024/1024
            d = den/1024/1024
        }
        if(dataType.equals("Gbs"))
        {
            n = num/1024/1024/1024
            d = den/1024/1024/1024
        }

    for(int i = 0; i < 50; i++){
        if( i < (percent/2)){
            bar += "|";
        }else if( i == (percent/2)){
            bar += ">";
        }else{
            bar += " ";
        }
    }

    def message = (percent == 100) ? "Complete!" : "processed..."

    bar += "]   " + df.format(percent) + "%    " + df.format(n) + " of " + df.format(d) + " $dataType $message " + HSTools.calculateTime(startDate);
    print "\r" + bar;
}

Output:

Loading data for: Patient_Contacts_Data.txt Creating Scanner and setting delimiter: \|^@*^@r^@*^@\| [||||||||||||||||||||||||||||||||||||||||||||||||||] 100.00% 1.60 of 1.60 Mbs Complete! 0:0:0:5656 Records Processed: 5857 Elapsed Time: 0:0:0:56

For the most part my code appears to be working, however occasionally the seconds appear to double up on themselves.

Any thoughts on what could be causing this? Or suggestions on how to better format this TimeDuration?

Was it helpful?

Solution

The problem with your code is that previous output is not deleted. Since your output is not formatted to a fixed size, shorter output that appears later will not overwrite the last characters from the previous output.

For instance, when duration goes from 59 seconds to 1 minute, it changes from something like this:

[|||||||||||||||||           ]   59.10%    .58 of .98 Kbs processed... 0:0:0:59

to something like this:

[|||||||||||||||||           ]   59.20%    .58 of .98 Kbs processed... 0:0:1:0

But since the first line is longer than the second, the actual output will look like this:

[|||||||||||||||||           ]   59.20%    .58 of .98 Kbs processed... 0:0:1:09

You can solve it with either appending some whitespace, or by making the time output of a fixed size (e.g. always use 2 digits for seconds, minutes, hours, etc.).

Note: When running from within IntelliJ IDEA the remaining part of the line was actually removed, but when running from a shell it was not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top