FileDocCategorySizeDatePackage
TimeUtils.javaAPI DocAndroid 5.1 API5905Thu Mar 12 22:22:56 GMT 2015android.support.v4.util

TimeUtils

public class TimeUtils extends Object
Helper for accessing features in {@link android.util.TimeUtils} introduced after API level 4 in a backwards compatible fashion.
hide

Fields Summary
public static final int
HUNDRED_DAY_FIELD_LEN
private static final int
SECONDS_PER_MINUTE
private static final int
SECONDS_PER_HOUR
private static final int
SECONDS_PER_DAY
private static final Object
sFormatSync
private static char[]
sFormatStr
Constructors Summary
Methods Summary
private static intaccumField(int amt, int suffix, boolean always, int zeropad)

    
               
        if (amt > 99 || (always && zeropad >= 3)) {
            return 3+suffix;
        }
        if (amt > 9 || (always && zeropad >= 2)) {
            return 2+suffix;
        }
        if (always || amt > 0) {
            return 1+suffix;
        }
        return 0;
    
public static voidformatDuration(long duration, java.lang.StringBuilder builder)

hide
Just for debugging; not internationalized.

        synchronized (sFormatSync) {
            int len = formatDurationLocked(duration, 0);
            builder.append(sFormatStr, 0, len);
        }
    
public static voidformatDuration(long duration, java.io.PrintWriter pw, int fieldLen)

hide
Just for debugging; not internationalized.

        synchronized (sFormatSync) {
            int len = formatDurationLocked(duration, fieldLen);
            pw.print(new String(sFormatStr, 0, len));
        }
    
public static voidformatDuration(long duration, java.io.PrintWriter pw)

hide
Just for debugging; not internationalized.

        formatDuration(duration, pw, 0);
    
public static voidformatDuration(long time, long now, java.io.PrintWriter pw)

hide
Just for debugging; not internationalized.

        if (time == 0) {
            pw.print("--");
            return;
        }
        formatDuration(time-now, pw, 0);
    
private static intformatDurationLocked(long duration, int fieldLen)

        if (sFormatStr.length < fieldLen) {
            sFormatStr = new char[fieldLen];
        }
        
        char[] formatStr = sFormatStr;
        
        if (duration == 0) {
            int pos = 0;
            fieldLen -= 1;
            while (pos < fieldLen) {
                formatStr[pos] = ' ";
            }
            formatStr[pos] = '0";
            return pos+1;
        }
        
        char prefix;
        if (duration > 0) {
            prefix = '+";
        } else {
            prefix = '-";
            duration = -duration;
        }

        int millis = (int)(duration%1000);
        int seconds = (int) Math.floor(duration / 1000);
        int days = 0, hours = 0, minutes = 0;

        if (seconds > SECONDS_PER_DAY) {
            days = seconds / SECONDS_PER_DAY;
            seconds -= days * SECONDS_PER_DAY;
        }
        if (seconds > SECONDS_PER_HOUR) {
            hours = seconds / SECONDS_PER_HOUR;
            seconds -= hours * SECONDS_PER_HOUR;
        }
        if (seconds > SECONDS_PER_MINUTE) {
            minutes = seconds / SECONDS_PER_MINUTE;
            seconds -= minutes * SECONDS_PER_MINUTE;
        }

        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);
            myLen += accumField(millis, 2, true, myLen > 0 ? 3 : 0) + 1;
            while (myLen < fieldLen) {
                formatStr[pos] = ' ";
                pos++;
                myLen++;
            }
        }
        
        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);
        pos = printField(formatStr, millis, 'm", pos, true, (zeropad && pos != start) ? 3 : 0);
        formatStr[pos] = 's";
        return pos + 1;
    
private static intprintField(char[] formatStr, int amt, char suffix, int pos, boolean always, int zeropad)

        if (always || amt > 0) {
            final int startPos = pos;
            if ((always && zeropad >= 3) || amt > 99) {
                int dig = amt/100;
                formatStr[pos] = (char)(dig + '0");
                pos++;
                amt -= (dig*100);
            }
            if ((always && zeropad >= 2) || amt > 9 || startPos != pos) {
                int dig = amt/10;
                formatStr[pos] = (char)(dig + '0");
                pos++;
                amt -= (dig*10);
            }
            formatStr[pos] = (char)(amt + '0");
            pos++;
            formatStr[pos] = suffix;
            pos++;
        }
        return pos;