Source code
package android.support.v4.util;
import java.io.PrintWriter;
public class TimeUtils {
public static final int HUNDRED_DAY_FIELD_LEN = 19;
private static final int SECONDS_PER_DAY = 86400;
private static final int SECONDS_PER_HOUR = 3600;
private static final int SECONDS_PER_MINUTE = 60;
private static char[] sFormatStr = new char[24];
private static final Object sFormatSync = new Object();
private static int accumField(int amt, int suffix, boolean always, int zeropad) {
if (amt > 99 || (always && zeropad >= 3)) {
return suffix + 3;
}
if (amt > 9 || (always && zeropad >= 2)) {
return suffix + 2;
}
if (always || amt > 0) {
return suffix + 1;
}
return 0;
}
private static int printField(char[] formatStr, int amt, char suffix, int pos, boolean always, int zeropad) {
if (!always && amt <= 0) {
return pos;
}
int startPos = pos;
if ((always && zeropad >= 3) || amt > 99) {
int dig = amt / 100;
formatStr[pos] = (char) (dig + 48);
pos++;
amt -= dig * 100;
}
if ((always && zeropad >= 2) || amt > 9 || startPos != pos) {
dig = amt / 10;
formatStr[pos] = (char) (dig + 48);
pos++;
amt -= dig * 10;
}
formatStr[pos] = (char) (amt + 48);
pos++;
formatStr[pos] = suffix;
return pos + 1;
}
private static int formatDurationLocked(long duration, int fieldLen) {
if (sFormatStr.length < fieldLen) {
sFormatStr = new char[fieldLen];
}
char[] formatStr = sFormatStr;
if (duration == 0) {
fieldLen--;
while (0 < fieldLen) {
formatStr[0] = ' ';
}
formatStr[0] = '0';
return 1;
}
char prefix;
if (duration > 0) {
prefix = '+';
} else {
prefix = '-';
duration = -duration;
}
int millis = (int) (duration % 1000);
int seconds = (int) Math.floor((double) (duration / 1000));
int days = 0;
int hours = 0;
int minutes = 0;
if (seconds > SECONDS_PER_DAY) {
days = seconds / SECONDS_PER_DAY;
seconds -= SECONDS_PER_DAY * days;
}
if (seconds > SECONDS_PER_HOUR) {
hours = seconds / SECONDS_PER_HOUR;
seconds -= hours * SECONDS_PER_HOUR;
}
if (seconds > 60) {
minutes = seconds / 60;
seconds -= minutes * 60;
}
int pos = 0;
if (fieldLen != 0) {
int myLen = accumField(days, 1, false, 0);
myLen += accumField(hours, 1, myLen > 0, 2);
myLen += accumField(minutes, 1, myLen > 0, 2);
myLen += accumField(seconds, 1, myLen > 0, 2);
for (myLen += accumField(millis, 2, true, myLen > 0 ? 3 : 0) + 1; myLen < fieldLen; myLen++) {
formatStr[pos] = ' ';
pos++;
}
}
formatStr[pos] = prefix;
pos++;
int start = pos;
boolean zeropad = fieldLen != 0;
pos = printField(formatStr, days, 'd', pos, false, 0);
pos = printField(formatStr, hours, 'h', pos, pos != start, zeropad ? 2 : 0);
pos = printField(formatStr, minutes, 'm', pos, pos != start, zeropad ? 2 : 0);
pos = printField(formatStr, seconds, 's', pos, pos != start, zeropad ? 2 : 0);
int i = (!zeropad || pos == start) ? 0 : 3;
pos = printField(formatStr, millis, 'm', pos, true, i);
formatStr[pos] = 's';
return pos + 1;
}
public static void formatDuration(long duration, StringBuilder builder) {
synchronized (sFormatSync) {
builder.append(sFormatStr, 0, formatDurationLocked(duration, 0));
}
}
public static void formatDuration(long duration, PrintWriter pw, int fieldLen) {
synchronized (sFormatSync) {
pw.print(new String(sFormatStr, 0, formatDurationLocked(duration, fieldLen)));
}
}
public static void formatDuration(long duration, PrintWriter pw) {
formatDuration(duration, pw, 0);
}
public static void formatDuration(long time, long now, PrintWriter pw) {
if (time == 0) {
pw.print("--");
} else {
formatDuration(time - now, pw, 0);
}
}
}