1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-02 08:25:02 -04:00
filebot/source/net/sourceforge/tuned/TimeIntervalFormat.java

52 lines
979 B
Java

package net.sourceforge.tuned;
import java.util.TreeMap;
public class TimeIntervalFormat {
private static TreeMap<Long, String> unitMap = new TreeMap<Long, String>();
static {
unitMap.put(1L, "ms");
unitMap.put(1000L, "s");
unitMap.put(60 * 1000L, "m");
unitMap.put(60 * 60 * 1000L, "h");
}
public static String format(long millis, boolean zerounits) {
boolean negativ = false;
if (millis < 0) {
millis = Math.abs(millis);
negativ = true;
}
StringBuffer sb = new StringBuffer();
for (long unitBaseTime : unitMap.descendingKeySet()) {
int quotient = (int) (millis / unitBaseTime);
boolean isLastKey = (unitBaseTime == unitMap.firstKey());
if (zerounits || quotient != 0 || isLastKey) {
sb.append(quotient + unitMap.get(unitBaseTime));
if (!isLastKey)
;
sb.append(" ");
}
millis -= quotient * unitBaseTime;
}
if (negativ)
sb.insert(0, "-");
return sb.toString();
}
}