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

Commit 315a7c03 authored by Roozbeh Pournader's avatar Roozbeh Pournader Committed by Elliott Hughes
Browse files

Use proper digits in formatElapsedTime and format3339

Use getZeroDigit() instead of a hard-coded '0' for formatting times using
formatElapsedTime, so locales with different digits like Arabic and Persian
could display the elapsed time properly. This is visible in Settings' list
of running apps.

Also changed android.text.format.Time's format3339 method to always use ASCII
digits, irrespective of the locale.

Change-Id: I731c96c21b3712ec347d9526e4ec3fe884dec276
parent 4dff2ab5
Loading
Loading
Loading
Loading
+22 −32
Original line number Diff line number Diff line
@@ -45,7 +45,6 @@ public class DateUtils

    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_PADDING = '0';
    private static final char TIME_SEPARATOR = ':';


@@ -648,33 +647,36 @@ public class DateUtils
        }
    }

    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
     * 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;
            if (sb == null) {
                sb = new StringBuilder(8);
            } else {
                sb.setLength(0);
            }
            sb.append(hours);
            append(sb, hours, false, zeroDigit);
            sb.append(TIME_SEPARATOR);
            if (minutes < 10) {
                sb.append(TIME_PADDING);
            } else {
                sb.append(toDigitChar(minutes / 10));
            }
            sb.append(toDigitChar(minutes % 10));
            append(sb, minutes, true, zeroDigit);
            sb.append(TIME_SEPARATOR);
            if (seconds < 10) {
                sb.append(TIME_PADDING);
            } else {
                sb.append(toDigitChar(seconds / 10));
            }
            sb.append(toDigitChar(seconds % 10));
            append(sb, seconds, true, zeroDigit);
            return sb.toString();
        } else {
            return String.format(format, hours, minutes, seconds);
@@ -682,40 +684,28 @@ public class DateUtils
    }

    /**
     * Fast formatting of m:ss
     * 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;
            if (sb == null) {
                sb = new StringBuilder(8);
            } else {
                sb.setLength(0);
            }
            if (minutes < 10) {
                sb.append(TIME_PADDING);
            } else {
                sb.append(toDigitChar(minutes / 10));
            }
            sb.append(toDigitChar(minutes % 10));
            append(sb, minutes, false, zeroDigit);
            sb.append(TIME_SEPARATOR);
            if (seconds < 10) {
                sb.append(TIME_PADDING);
            } else {
                sb.append(toDigitChar(seconds / 10));
            }
            sb.append(toDigitChar(seconds % 10));
            append(sb, seconds, true, zeroDigit);
            return sb.toString();
        } else {
            return String.format(format, minutes, seconds);
        }
    }

    private static char toDigitChar(long digit) {
        return (char) (digit + '0');
    }

    /**
     * Format a date / time such that if the then is on the same day as now, it shows
     * just the time and if it's a different day, it shows just the date.
+3 −1
Original line number Diff line number Diff line
@@ -151,6 +151,8 @@ public class Time {
    private static String sDateTimeFormat;
    private static String sAm;
    private static String sPm;

    // Referenced by native code.
    private static String sDateCommand = "%a %b %e %H:%M:%S %Z %Y";

    /**
@@ -673,7 +675,7 @@ public class Time {
            int minutes = (offset % 3600) / 60;
            int hours = offset / 3600;

            return String.format("%s%s%02d:%02d", base, sign, hours, minutes);
            return String.format(Locale.US, "%s%s%02d:%02d", base, sign, hours, minutes);
        }
    }