Merge "Fix issue #3224616: TimeUtils.formatDuration() can drop 0s." into gingerbread
This commit is contained in:
committed by
Android (Google) Code Review
commit
5261cea2e0
@ -158,18 +158,17 @@ public class TimeUtils {
|
|||||||
static private int printField(char[] formatStr, int amt, char suffix, int pos,
|
static private int printField(char[] formatStr, int amt, char suffix, int pos,
|
||||||
boolean always, int zeropad) {
|
boolean always, int zeropad) {
|
||||||
if (always || amt > 0) {
|
if (always || amt > 0) {
|
||||||
|
final int startPos = pos;
|
||||||
if ((always && zeropad >= 3) || amt > 99) {
|
if ((always && zeropad >= 3) || amt > 99) {
|
||||||
int dig = amt/100;
|
int dig = amt/100;
|
||||||
formatStr[pos] = (char)(dig + '0');
|
formatStr[pos] = (char)(dig + '0');
|
||||||
pos++;
|
pos++;
|
||||||
always = true;
|
|
||||||
amt -= (dig*100);
|
amt -= (dig*100);
|
||||||
}
|
}
|
||||||
if ((always && zeropad >= 2) || amt > 9) {
|
if ((always && zeropad >= 2) || amt > 9 || startPos != pos) {
|
||||||
int dig = amt/10;
|
int dig = amt/10;
|
||||||
formatStr[pos] = (char)(dig + '0');
|
formatStr[pos] = (char)(dig + '0');
|
||||||
pos++;
|
pos++;
|
||||||
always = true;
|
|
||||||
amt -= (dig*10);
|
amt -= (dig*10);
|
||||||
}
|
}
|
||||||
formatStr[pos] = (char)(amt + '0');
|
formatStr[pos] = (char)(amt + '0');
|
||||||
|
@ -429,4 +429,22 @@ public class TimeUtilsTest extends TestCase {
|
|||||||
c.getTimeInMillis(),
|
c.getTimeInMillis(),
|
||||||
country);
|
country);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testFormatDuration() {
|
||||||
|
assertFormatDuration("0", 0);
|
||||||
|
assertFormatDuration("-1ms", -1);
|
||||||
|
assertFormatDuration("+1ms", 1);
|
||||||
|
assertFormatDuration("+10ms", 10);
|
||||||
|
assertFormatDuration("+100ms", 100);
|
||||||
|
assertFormatDuration("+101ms", 101);
|
||||||
|
assertFormatDuration("+330ms", 330);
|
||||||
|
assertFormatDuration("+1s330ms", 1330);
|
||||||
|
assertFormatDuration("+10s24ms", 10024);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertFormatDuration(String expected, long duration) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
TimeUtils.formatDuration(duration, sb);
|
||||||
|
assertEquals("formatDuration(" + duration + ")", expected, sb.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user