Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a43e9b7c authored by Elliott Hughes's avatar Elliott Hughes Committed by Android Git Automerger
Browse files

am 6647f1bd: am c749261f: Merge "Fix DateUtils.formatElapsedTime."

* commit '6647f1bd':
  Fix DateUtils.formatElapsedTime.
parents f4c50a87 6647f1bd
Loading
Loading
Loading
Loading
+18 −71
Original line number Original line Diff line number Diff line
@@ -43,11 +43,6 @@ public class DateUtils
    private static String sElapsedFormatMMSS;
    private static String sElapsedFormatMMSS;
    private static String sElapsedFormatHMMSS;
    private static String sElapsedFormatHMMSS;


    private static final String FAST_FORMAT_HMMSS = "%1$d:%2$02d:%3$02d";
    private static final String FAST_FORMAT_MMSS = "%1$02d:%2$02d";
    private static final char TIME_SEPARATOR = ':';


    public static final long SECOND_IN_MILLIS = 1000;
    public static final long SECOND_IN_MILLIS = 1000;
    public static final long MINUTE_IN_MILLIS = SECOND_IN_MILLIS * 60;
    public static final long MINUTE_IN_MILLIS = SECOND_IN_MILLIS * 60;
    public static final long HOUR_IN_MILLIS = MINUTE_IN_MILLIS * 60;
    public static final long HOUR_IN_MILLIS = MINUTE_IN_MILLIS * 60;
@@ -640,19 +635,18 @@ public class DateUtils
    }
    }


    /**
    /**
     * Formats an elapsed time in the form "MM:SS" or "H:MM:SS"
     * Formats an elapsed time in a format like "MM:SS" or "H:MM:SS" (using a form
     * for display on the call-in-progress screen.
     * suited to the current locale), similar to that used on the call-in-progress
     * screen.
     *
     *
     * @param recycle {@link StringBuilder} to recycle, if possible
     * @param recycle {@link StringBuilder} to recycle, or null to use a temporary one.
     * @param elapsedSeconds the elapsed time in seconds.
     * @param elapsedSeconds the elapsed time in seconds.
     */
     */
    public static String formatElapsedTime(StringBuilder recycle, long elapsedSeconds) {
    public static String formatElapsedTime(StringBuilder recycle, long elapsedSeconds) {
        initFormatStrings();
        // Break the elapsed seconds into hours, minutes, and seconds.

        long hours = 0;
        long hours = 0;
        long minutes = 0;
        long minutes = 0;
        long seconds = 0;
        long seconds = 0;

        if (elapsedSeconds >= 3600) {
        if (elapsedSeconds >= 3600) {
            hours = elapsedSeconds / 3600;
            hours = elapsedSeconds / 3600;
            elapsedSeconds -= hours * 3600;
            elapsedSeconds -= hours * 3600;
@@ -663,70 +657,23 @@ public class DateUtils
        }
        }
        seconds = elapsedSeconds;
        seconds = elapsedSeconds;


        String result;
        // Create a StringBuilder if we weren't given one to recycle.
        if (hours > 0) {
        // TODO: if we cared, we could have a thread-local temporary StringBuilder.
            return formatElapsedTime(recycle, sElapsedFormatHMMSS, hours, minutes, seconds);
        } else {
            return formatElapsedTime(recycle, sElapsedFormatMMSS, minutes, seconds);
        }
    }

    private static void append(StringBuilder sb, long value, boolean pad, char zeroDigit) {
        if (value < 10) {
            if (pad) {
                sb.append(zeroDigit);
            }
        } else {
            sb.append((char) (zeroDigit + (value / 10)));
        }
        sb.append((char) (zeroDigit + (value % 10)));
    }

    /**
     * Fast formatting of h:mm:ss.
     */
    private static String formatElapsedTime(StringBuilder recycle, String format, long hours,
            long minutes, long seconds) {
        if (FAST_FORMAT_HMMSS.equals(format)) {
            char zeroDigit = LocaleData.get(Locale.getDefault()).zeroDigit;

        StringBuilder sb = recycle;
        StringBuilder sb = recycle;
        if (sb == null) {
        if (sb == null) {
            sb = new StringBuilder(8);
            sb = new StringBuilder(8);
        } else {
        } else {
            sb.setLength(0);
            sb.setLength(0);
        }
        }
            append(sb, hours, false, zeroDigit);
            sb.append(TIME_SEPARATOR);
            append(sb, minutes, true, zeroDigit);
            sb.append(TIME_SEPARATOR);
            append(sb, seconds, true, zeroDigit);
            return sb.toString();
        } else {
            return String.format(format, hours, minutes, seconds);
        }
    }

    /**
     * Fast formatting of mm:ss.
     */
    private static String formatElapsedTime(StringBuilder recycle, String format, long minutes,
            long seconds) {
        if (FAST_FORMAT_MMSS.equals(format)) {
            char zeroDigit = LocaleData.get(Locale.getDefault()).zeroDigit;


            StringBuilder sb = recycle;
        // Format the broken-down time in a locale-appropriate way.
            if (sb == null) {
        // TODO: use icu4c when http://unicode.org/cldr/trac/ticket/3407 is fixed.
                sb = new StringBuilder(8);
        Formatter f = new Formatter(sb, Locale.getDefault());
            } else {
        initFormatStrings();
                sb.setLength(0);
        if (hours > 0) {
            }
            return f.format(sElapsedFormatHMMSS, hours, minutes, seconds).toString();
            append(sb, minutes, false, zeroDigit);
            sb.append(TIME_SEPARATOR);
            append(sb, seconds, true, zeroDigit);
            return sb.toString();
        } else {
        } else {
            return String.format(format, minutes, seconds);
            return f.format(sElapsedFormatMMSS, minutes, seconds).toString();
        }
        }
    }
    }