ICU-3487 duration (handle milliseconds)

X-SVN-Rev: 22512
This commit is contained in:
Steven R. Loomis 2007-08-24 16:34:29 +00:00
parent 0edc6125a1
commit ccceb4f4a8
2 changed files with 30 additions and 3 deletions

View File

@ -140,7 +140,6 @@ public class ICUDurationTest extends TestFmwk {
} else {
errln("FAIL: got " + out + " wanted " + expected + " from " + d);
}
}
@ -162,6 +161,13 @@ public class ICUDurationTest extends TestFmwk {
// from BD req's
"en", "PT2H46M40S", "2 hours, 46 minutes, and 40 seconds",
"it", "PT2H46M40S", "due ore, 46 minuti e 40 secondi",
// more cases
"en", "PT10S", "10 seconds",
"en", "PT88M70S", "88 minutes and 70 seconds",
"en", "PT10.100S", "10 seconds and 100 milliseconds",
"en", "-PT10S", "10 seconds",
"en", "PT0H5M0S", "5 minutes and 0 seconds"
};
for(int n=0;n<cases.length;n+=3) {

View File

@ -142,10 +142,31 @@ public class BasicDurationFormat extends DurationFormat {
} else {
sawNonZero = true;
}
float floatVal = n.floatValue();
// is there a 'secondary' unit to set?
TimeUnit alternateUnit = null;
float alternateVal = 0;
// see if there is a fractional part
if(outFields[i]==TimeUnit.SECOND) {
double fullSeconds = floatVal;
double intSeconds = Math.floor(floatVal);
double millis = (fullSeconds-intSeconds)*1000.0;
if(millis > 0.0) {
alternateUnit = TimeUnit.MILLISECOND;
alternateVal=(float)millis;
floatVal=(float)intSeconds;
}
}
if(p == null) {
p = Period.at(n.floatValue(), outFields[i]);
p = Period.at(floatVal, outFields[i]);
} else {
p = p.and(n.floatValue(), outFields[i]);
p = p.and(floatVal, outFields[i]);
}
if(alternateUnit != null) {
p = p.and(alternateVal, alternateUnit); // add in MILLISECONDs
}
}
}