Skip to content

Commit 5261cea

Browse files
Dianne HackbornAndroid (Google) Code Review
authored andcommitted
Merge "Fix issue #3224616: TimeUtils.formatDuration() can drop 0s." into gingerbread
2 parents 0c8ad64 + f5f7510 commit 5261cea

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

core/java/android/util/TimeUtils.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,17 @@ static private int accumField(int amt, int suffix, boolean always, int zeropad)
158158
static private int printField(char[] formatStr, int amt, char suffix, int pos,
159159
boolean always, int zeropad) {
160160
if (always || amt > 0) {
161+
final int startPos = pos;
161162
if ((always && zeropad >= 3) || amt > 99) {
162163
int dig = amt/100;
163164
formatStr[pos] = (char)(dig + '0');
164165
pos++;
165-
always = true;
166166
amt -= (dig*100);
167167
}
168-
if ((always && zeropad >= 2) || amt > 9) {
168+
if ((always && zeropad >= 2) || amt > 9 || startPos != pos) {
169169
int dig = amt/10;
170170
formatStr[pos] = (char)(dig + '0');
171171
pos++;
172-
always = true;
173172
amt -= (dig*10);
174173
}
175174
formatStr[pos] = (char)(amt + '0');

core/tests/coretests/src/android/util/TimeUtilsTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,4 +429,22 @@ private static TimeZone guess(Calendar c, String country) {
429429
c.getTimeInMillis(),
430430
country);
431431
}
432+
433+
public void testFormatDuration() {
434+
assertFormatDuration("0", 0);
435+
assertFormatDuration("-1ms", -1);
436+
assertFormatDuration("+1ms", 1);
437+
assertFormatDuration("+10ms", 10);
438+
assertFormatDuration("+100ms", 100);
439+
assertFormatDuration("+101ms", 101);
440+
assertFormatDuration("+330ms", 330);
441+
assertFormatDuration("+1s330ms", 1330);
442+
assertFormatDuration("+10s24ms", 10024);
443+
}
444+
445+
private void assertFormatDuration(String expected, long duration) {
446+
StringBuilder sb = new StringBuilder();
447+
TimeUtils.formatDuration(duration, sb);
448+
assertEquals("formatDuration(" + duration + ")", expected, sb.toString());
449+
}
432450
}

0 commit comments

Comments
 (0)