Fix bug in Duration.toString.

The `toString` used the sign of `hours` to get a leading `-`
when the duration is negative. However, a negative duration of
less than one hour would therefore miss out on the sign,
because `-0` is not an integer.
Now handles sign explicitly.

Fixes #48841.

Change-Id: I0ab6d451faf1c76b838fc35a692f07c5b035d2a7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/241748
Reviewed-by: Nate Bosch <nbosch@google.com>
Commit-Queue: Lasse Nielsen <lrn@google.com>
This commit is contained in:
Lasse R.H. Nielsen 2022-04-25 14:37:42 +00:00 committed by Commit Bot
parent db0d9b1852
commit 0f0f045128
3 changed files with 36 additions and 1 deletions

View file

@ -331,6 +331,7 @@ class Duration implements Comparable<Duration> {
/// ```
String toString() {
var microseconds = inMicroseconds;
var sign = (microseconds < 0) ? "-" : "";
var hours = microseconds ~/ microsecondsPerHour;
microseconds = microseconds.remainder(microsecondsPerHour);
@ -348,7 +349,7 @@ class Duration implements Comparable<Duration> {
var secondsPadding = seconds < 10 ? "0" : "";
var paddedMicroseconds = microseconds.toString().padLeft(6, "0");
return "$hours:"
return "$sign${hours.abs()}:"
"$minutesPadding$minutes:"
"$secondsPadding$seconds.$paddedMicroseconds";
}

View file

@ -298,4 +298,21 @@ main() {
Expect.equals(d2, -d1);
Expect.equals(d1, -d2);
// Regression test for http://dartbug.com/48841
d = const Duration(minutes: -1);
Expect.equals("-0:01:00.000000", d.toString());
d = const Duration(seconds: -1);
Expect.equals("-0:00:01.000000", d.toString());
d = const Duration(milliseconds: -1);
Expect.equals("-0:00:00.001000", d.toString());
d = const Duration(microseconds: -1);
Expect.equals("-0:00:00.000001", d.toString());
d = const Duration(microseconds: -9223372036854775808); // Min 64-bit int.
// Not checking precise values, because they will be off on the web.
Expect.equals("-2562047788:00:54.775808", d.toString());
Expect.isTrue(d.toString().startsWith("-"));
Expect.isFalse(d.toString().startsWith("--"));
d = const Duration(minutes: -0); // is -0.0 on web.
Expect.equals("0:00:00.000000", d.toString());
}

View file

@ -300,4 +300,21 @@ main() {
Expect.equals(d2, -d1);
Expect.equals(d1, -d2);
// Regression test for http://dartbug.com/48841
d = const Duration(minutes: -1);
Expect.equals("-0:01:00.000000", d.toString());
d = const Duration(seconds: -1);
Expect.equals("-0:00:01.000000", d.toString());
d = const Duration(milliseconds: -1);
Expect.equals("-0:00:00.001000", d.toString());
d = const Duration(microseconds: -1);
Expect.equals("-0:00:00.000001", d.toString());
d = const Duration(microseconds: -9223372036854775808); // Min 64-bit int.
// Not checking precise values, because they will be off on the web.
Expect.equals("-2562047788:00:54.775808", d.toString());
Expect.isTrue(d.toString().startsWith("-"));
Expect.isFalse(d.toString().startsWith("--"));
d = const Duration(minutes: -0); // is -0.0 on web.
Expect.equals("0:00:00.000000", d.toString());
}