Skip to content

Commit

Permalink
GROOVY-5751: Adding milliseconds with TimeCategory gives incorrect to…
Browse files Browse the repository at this point in the history
…String
  • Loading branch information
paulk-asert committed Oct 12, 2012
1 parent c5b708f commit d7a7c33
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/main/groovy/time/BaseDuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ public String toString() {
if (this.hours != 0) buffer.add(this.hours + " hours");
if (this.minutes != 0) buffer.add(this.minutes + " minutes");

if (this.seconds != 0 || this.millis != 0)
buffer.add((seconds == 0 ? (millis < 0 ? "-0" : "0") : seconds) + "." + StringGroovyMethods.padLeft("" + Math.abs(millis), 3, "0") + " seconds");
if (this.seconds != 0 || this.millis != 0) {
int norm_millis = millis % 1000;
int norm_seconds = seconds + DefaultGroovyMethods.intdiv(millis - norm_millis, 1000).intValue();
buffer.add((norm_seconds == 0 ? (norm_millis < 0 ? "-0" : "0") : norm_seconds) + "." + StringGroovyMethods.padLeft("" + Math.abs(norm_millis), 3, "0") + " seconds");
}

if (buffer.size() != 0) {
return DefaultGroovyMethods.join(buffer, ", ");
Expand Down
7 changes: 7 additions & 0 deletions src/test/groovy/time/TimeCategoryTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,11 @@ class TimeCategoryTest extends GroovyTestCase {
}
}

void testToStringForOverflow() {
use(TimeCategory) {
def t = 800.milliseconds + 300.milliseconds
assert t.toString() == '1.100 seconds'
}
}

}

0 comments on commit d7a7c33

Please sign in to comment.