Skip to content

Commit 401b70f

Browse files
authored
Fix Formatting Negative Times (#580)
It turns out that the new formatting code accidentally relied on the fact that the sign for both the seconds and the subsecond nanoseconds are always the same... and they almost always are. The problem is that unlike floating point numbers, integers can't represent a separate positive and negative 0. So if you have 0 seconds, the sign of the nanoseconds actually matters. So you always have to take its sign into account as well.
1 parent 9abf7cf commit 401b70f

6 files changed

Lines changed: 8 additions & 8 deletions

File tree

src/timing/formatter/complete.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl Display for Inner {
4848
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
4949
if let Some(time) = self.0 {
5050
let (total_seconds, nanoseconds) = time.to_seconds_and_subsec_nanoseconds();
51-
let (total_seconds, nanoseconds) = if total_seconds < 0 {
51+
let (total_seconds, nanoseconds) = if (total_seconds | nanoseconds as i64) < 0 {
5252
// Since, this Formatter is used for writing out split files, we
5353
// have to use an ASCII Minus here.
5454
f.write_str(ASCII_MINUS)?;

src/timing/formatter/days.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ impl TimeFormatter<'_> for Days {
4545
impl Display for Inner {
4646
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
4747
if let Some(time) = self.time {
48-
let total_seconds = time.to_duration().whole_seconds();
49-
let total_seconds = if total_seconds < 0 {
48+
let (total_seconds, nanoseconds) = time.to_seconds_and_subsec_nanoseconds();
49+
let total_seconds = if (total_seconds | nanoseconds as i64) < 0 {
5050
f.write_str(MINUS)?;
5151
(-total_seconds) as u64
5252
} else {

src/timing/formatter/delta.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl Display for Inner {
7474
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
7575
if let Some(time) = self.time {
7676
let (total_seconds, nanoseconds) = time.to_seconds_and_subsec_nanoseconds();
77-
let (total_seconds, nanoseconds) = if total_seconds < 0 {
77+
let (total_seconds, nanoseconds) = if (total_seconds | nanoseconds as i64) < 0 {
7878
f.write_str(MINUS)?;
7979
((-total_seconds) as u64, (-nanoseconds) as u32)
8080
} else {

src/timing/formatter/regular.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl Display for Inner {
6868
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
6969
if let Some(time) = self.time {
7070
let (total_seconds, nanoseconds) = time.to_seconds_and_subsec_nanoseconds();
71-
let (total_seconds, nanoseconds) = if total_seconds < 0 {
71+
let (total_seconds, nanoseconds) = if (total_seconds | nanoseconds as i64) < 0 {
7272
f.write_str(MINUS)?;
7373
((-total_seconds) as u64, (-nanoseconds) as u32)
7474
} else {

src/timing/formatter/segment_time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl Display for Inner {
6666
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
6767
if let Some(time) = self.time {
6868
let (total_seconds, nanoseconds) = time.to_seconds_and_subsec_nanoseconds();
69-
let (total_seconds, nanoseconds) = if total_seconds < 0 {
69+
let (total_seconds, nanoseconds) = if (total_seconds | nanoseconds as i64) < 0 {
7070
f.write_str(MINUS)?;
7171
((-total_seconds) as u64, (-nanoseconds) as u32)
7272
} else {

src/timing/formatter/timer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ impl TimeFormatter<'_> for Time {
6969
impl Display for TimeInner {
7070
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
7171
if let Some(time) = self.time {
72-
let total_seconds = time.to_duration().whole_seconds();
73-
let total_seconds = if total_seconds < 0 {
72+
let (total_seconds, nanoseconds) = time.to_seconds_and_subsec_nanoseconds();
73+
let total_seconds = if (total_seconds | nanoseconds as i64) < 0 {
7474
f.write_str(MINUS)?;
7575
(-total_seconds) as u64
7676
} else {

0 commit comments

Comments
 (0)