Skip to content

Commit

Permalink
Fix Formatting Negative Times (#580)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
CryZe authored Oct 9, 2022
1 parent 9abf7cf commit 401b70f
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/timing/formatter/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Display for Inner {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
if let Some(time) = self.0 {
let (total_seconds, nanoseconds) = time.to_seconds_and_subsec_nanoseconds();
let (total_seconds, nanoseconds) = if total_seconds < 0 {
let (total_seconds, nanoseconds) = if (total_seconds | nanoseconds as i64) < 0 {
// Since, this Formatter is used for writing out split files, we
// have to use an ASCII Minus here.
f.write_str(ASCII_MINUS)?;
Expand Down
4 changes: 2 additions & 2 deletions src/timing/formatter/days.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ impl TimeFormatter<'_> for Days {
impl Display for Inner {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
if let Some(time) = self.time {
let total_seconds = time.to_duration().whole_seconds();
let total_seconds = if total_seconds < 0 {
let (total_seconds, nanoseconds) = time.to_seconds_and_subsec_nanoseconds();
let total_seconds = if (total_seconds | nanoseconds as i64) < 0 {
f.write_str(MINUS)?;
(-total_seconds) as u64
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/timing/formatter/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Display for Inner {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
if let Some(time) = self.time {
let (total_seconds, nanoseconds) = time.to_seconds_and_subsec_nanoseconds();
let (total_seconds, nanoseconds) = if total_seconds < 0 {
let (total_seconds, nanoseconds) = if (total_seconds | nanoseconds as i64) < 0 {
f.write_str(MINUS)?;
((-total_seconds) as u64, (-nanoseconds) as u32)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/timing/formatter/regular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Display for Inner {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
if let Some(time) = self.time {
let (total_seconds, nanoseconds) = time.to_seconds_and_subsec_nanoseconds();
let (total_seconds, nanoseconds) = if total_seconds < 0 {
let (total_seconds, nanoseconds) = if (total_seconds | nanoseconds as i64) < 0 {
f.write_str(MINUS)?;
((-total_seconds) as u64, (-nanoseconds) as u32)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/timing/formatter/segment_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Display for Inner {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
if let Some(time) = self.time {
let (total_seconds, nanoseconds) = time.to_seconds_and_subsec_nanoseconds();
let (total_seconds, nanoseconds) = if total_seconds < 0 {
let (total_seconds, nanoseconds) = if (total_seconds | nanoseconds as i64) < 0 {
f.write_str(MINUS)?;
((-total_seconds) as u64, (-nanoseconds) as u32)
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/timing/formatter/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ impl TimeFormatter<'_> for Time {
impl Display for TimeInner {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
if let Some(time) = self.time {
let total_seconds = time.to_duration().whole_seconds();
let total_seconds = if total_seconds < 0 {
let (total_seconds, nanoseconds) = time.to_seconds_and_subsec_nanoseconds();
let total_seconds = if (total_seconds | nanoseconds as i64) < 0 {
f.write_str(MINUS)?;
(-total_seconds) as u64
} else {
Expand Down

0 comments on commit 401b70f

Please sign in to comment.