diff options
author | Elliott Hughes <enh@google.com> | 2012-12-14 14:29:47 -0800 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2012-12-14 14:29:47 -0800 |
commit | 2eda18485cefc5dbc946d7eb32486a7bd67bf8af (patch) | |
tree | e01b7fea230447f6e3f45861b68d22f0365d62fd /core/java/android/text | |
parent | 40627de3044c4a23c20c2229cab20aab21ff2b03 (diff) | |
download | frameworks_base-2eda18485cefc5dbc946d7eb32486a7bd67bf8af.zip frameworks_base-2eda18485cefc5dbc946d7eb32486a7bd67bf8af.tar.gz frameworks_base-2eda18485cefc5dbc946d7eb32486a7bd67bf8af.tar.bz2 |
Fix DateUtils.formatElapsedTime.
More reuse of StringBuilders, less broken home-grown formatting code.
Long-term, we should hand this over to icu4c, but they're not ready yet.
Bug: http://code.google.com/p/android/issues/detail?id=41401
Bug: 7736688
Change-Id: Ib3c1e1aad05827df646aa18645cce19dffb7551f
Diffstat (limited to 'core/java/android/text')
-rw-r--r-- | core/java/android/text/format/DateUtils.java | 89 |
1 files changed, 18 insertions, 71 deletions
diff --git a/core/java/android/text/format/DateUtils.java b/core/java/android/text/format/DateUtils.java index 1060bd8..ddd62a8 100644 --- a/core/java/android/text/format/DateUtils.java +++ b/core/java/android/text/format/DateUtils.java @@ -43,11 +43,6 @@ public class DateUtils private static String sElapsedFormatMMSS; 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 MINUTE_IN_MILLIS = SECOND_IN_MILLIS * 60; public static final long HOUR_IN_MILLIS = MINUTE_IN_MILLIS * 60; @@ -616,19 +611,18 @@ public class DateUtils } /** - * Formats an elapsed time in the form "MM:SS" or "H:MM:SS" - * for display on the call-in-progress screen. + * Formats an elapsed time in a format like "MM:SS" or "H:MM:SS" (using a form + * 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. */ public static String formatElapsedTime(StringBuilder recycle, long elapsedSeconds) { - initFormatStrings(); - + // Break the elapsed seconds into hours, minutes, and seconds. long hours = 0; long minutes = 0; long seconds = 0; - if (elapsedSeconds >= 3600) { hours = elapsedSeconds / 3600; elapsedSeconds -= hours * 3600; @@ -639,70 +633,23 @@ public class DateUtils } seconds = elapsedSeconds; - String result; - if (hours > 0) { - return formatElapsedTime(recycle, sElapsedFormatHMMSS, hours, minutes, seconds); + // Create a StringBuilder if we weren't given one to recycle. + // TODO: if we cared, we could have a thread-local temporary StringBuilder. + StringBuilder sb = recycle; + if (sb == null) { + sb = new StringBuilder(8); } else { - return formatElapsedTime(recycle, sElapsedFormatMMSS, minutes, seconds); + sb.setLength(0); } - } - - 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; - if (sb == null) { - sb = new StringBuilder(8); - } else { - 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; - if (sb == null) { - sb = new StringBuilder(8); - } else { - sb.setLength(0); - } - append(sb, minutes, false, zeroDigit); - sb.append(TIME_SEPARATOR); - append(sb, seconds, true, zeroDigit); - return sb.toString(); + // Format the broken-down time in a locale-appropriate way. + // TODO: use icu4c when http://unicode.org/cldr/trac/ticket/3407 is fixed. + Formatter f = new Formatter(sb, Locale.getDefault()); + initFormatStrings(); + if (hours > 0) { + return f.format(sElapsedFormatHMMSS, hours, minutes, seconds).toString(); } else { - return String.format(format, minutes, seconds); + return f.format(sElapsedFormatMMSS, minutes, seconds).toString(); } } |