Skip to content

Commit 315a7c0

Browse files
roozbehpenh-google
authored andcommitted
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
1 parent 4dff2ab commit 315a7c0

File tree

2 files changed

+25
-33
lines changed

2 files changed

+25
-33
lines changed

core/java/android/text/format/DateUtils.java

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public class DateUtils
4545

4646
private static final String FAST_FORMAT_HMMSS = "%1$d:%2$02d:%3$02d";
4747
private static final String FAST_FORMAT_MMSS = "%1$02d:%2$02d";
48-
private static final char TIME_PADDING = '0';
4948
private static final char TIME_SEPARATOR = ':';
5049

5150

@@ -648,74 +647,65 @@ public static String formatElapsedTime(StringBuilder recycle, long elapsedSecond
648647
}
649648
}
650649

650+
private static void append(StringBuilder sb, long value, boolean pad, char zeroDigit) {
651+
if (value < 10) {
652+
if (pad) {
653+
sb.append(zeroDigit);
654+
}
655+
} else {
656+
sb.append((char) (zeroDigit + (value / 10)));
657+
}
658+
sb.append((char) (zeroDigit + (value % 10)));
659+
}
660+
651661
/**
652-
* Fast formatting of h:mm:ss
662+
* Fast formatting of h:mm:ss.
653663
*/
654664
private static String formatElapsedTime(StringBuilder recycle, String format, long hours,
655665
long minutes, long seconds) {
656666
if (FAST_FORMAT_HMMSS.equals(format)) {
667+
char zeroDigit = LocaleData.get(Locale.getDefault()).zeroDigit;
668+
657669
StringBuilder sb = recycle;
658670
if (sb == null) {
659671
sb = new StringBuilder(8);
660672
} else {
661673
sb.setLength(0);
662674
}
663-
sb.append(hours);
675+
append(sb, hours, false, zeroDigit);
664676
sb.append(TIME_SEPARATOR);
665-
if (minutes < 10) {
666-
sb.append(TIME_PADDING);
667-
} else {
668-
sb.append(toDigitChar(minutes / 10));
669-
}
670-
sb.append(toDigitChar(minutes % 10));
677+
append(sb, minutes, true, zeroDigit);
671678
sb.append(TIME_SEPARATOR);
672-
if (seconds < 10) {
673-
sb.append(TIME_PADDING);
674-
} else {
675-
sb.append(toDigitChar(seconds / 10));
676-
}
677-
sb.append(toDigitChar(seconds % 10));
679+
append(sb, seconds, true, zeroDigit);
678680
return sb.toString();
679681
} else {
680682
return String.format(format, hours, minutes, seconds);
681683
}
682684
}
683685

684686
/**
685-
* Fast formatting of m:ss
687+
* Fast formatting of mm:ss.
686688
*/
687689
private static String formatElapsedTime(StringBuilder recycle, String format, long minutes,
688690
long seconds) {
689691
if (FAST_FORMAT_MMSS.equals(format)) {
692+
char zeroDigit = LocaleData.get(Locale.getDefault()).zeroDigit;
693+
690694
StringBuilder sb = recycle;
691695
if (sb == null) {
692696
sb = new StringBuilder(8);
693697
} else {
694698
sb.setLength(0);
695699
}
696-
if (minutes < 10) {
697-
sb.append(TIME_PADDING);
698-
} else {
699-
sb.append(toDigitChar(minutes / 10));
700-
}
701-
sb.append(toDigitChar(minutes % 10));
700+
append(sb, minutes, false, zeroDigit);
702701
sb.append(TIME_SEPARATOR);
703-
if (seconds < 10) {
704-
sb.append(TIME_PADDING);
705-
} else {
706-
sb.append(toDigitChar(seconds / 10));
707-
}
708-
sb.append(toDigitChar(seconds % 10));
702+
append(sb, seconds, true, zeroDigit);
709703
return sb.toString();
710704
} else {
711705
return String.format(format, minutes, seconds);
712706
}
713707
}
714708

715-
private static char toDigitChar(long digit) {
716-
return (char) (digit + '0');
717-
}
718-
719709
/**
720710
* Format a date / time such that if the then is on the same day as now, it shows
721711
* just the time and if it's a different day, it shows just the date.

core/java/android/text/format/Time.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ public class Time {
151151
private static String sDateTimeFormat;
152152
private static String sAm;
153153
private static String sPm;
154+
155+
// Referenced by native code.
154156
private static String sDateCommand = "%a %b %e %H:%M:%S %Z %Y";
155157

156158
/**
@@ -673,7 +675,7 @@ public String format3339(boolean allDay) {
673675
int minutes = (offset % 3600) / 60;
674676
int hours = offset / 3600;
675677

676-
return String.format("%s%s%02d:%02d", base, sign, hours, minutes);
678+
return String.format(Locale.US, "%s%s%02d:%02d", base, sign, hours, minutes);
677679
}
678680
}
679681

0 commit comments

Comments
 (0)