@@ -658,14 +658,9 @@ fn format_timestamp(seconds: i64, nanoseconds: u32, precision: Precision) -> Str
658658 let divisor = 10_i128 . pow ( ( 9 - precision) as u32 ) ;
659659 // Integer seconds use the floor, but fractional output truncates toward zero.
660660 let value = total_nanoseconds / divisor;
661- let result = format_scaled_decimal ( value, precision) ;
662- if total_nanoseconds < 0 && value == 0 {
663- format ! ( "-{result}" )
664- } else {
665- result
666- }
661+ format_scaled_decimal ( value, precision, total_nanoseconds < 0 )
667662 } else {
668- let mut result = format_scaled_decimal ( total_nanoseconds, 9 ) ;
663+ let mut result = format_scaled_decimal ( total_nanoseconds, 9 , total_nanoseconds < 0 ) ;
669664 result. push_str ( & "0" . repeat ( precision - 9 ) ) ;
670665 result
671666 }
@@ -680,16 +675,12 @@ fn system_time_to_timestamp(time: SystemTime) -> (i64, u32) {
680675 ( seconds, nanoseconds)
681676}
682677
683- fn format_scaled_decimal ( value : i128 , precision : usize ) -> String {
684- if precision == 0 {
685- return value. to_string ( ) ;
686- }
687-
678+ fn format_scaled_decimal ( value : i128 , precision : usize , negative : bool ) -> String {
688679 let scale = 10_u128 . pow ( precision as u32 ) ;
689680 let magnitude = value. unsigned_abs ( ) ;
690681 let whole = magnitude / scale;
691682 let fraction = magnitude % scale;
692- let sign = if value < 0 { "-" } else { "" } ;
683+ let sign = if negative { "-" } else { "" } ;
693684 format ! ( "{sign}{whole}.{fraction:0>precision$}" )
694685}
695686
@@ -1753,8 +1744,10 @@ mod tests {
17531744 ( -3 , 765_432_109 , Precision :: Number ( 0 ) , "-3" ) ,
17541745 ( -3 , 765_432_109 , Precision :: Number ( 2 ) , "-2.23" ) ,
17551746 ( -3 , 765_432_109 , Precision :: Number ( 6 ) , "-2.234567" ) ,
1747+ ( -1 , 999_999_999 , Precision :: Number ( 1 ) , "-0.0" ) ,
17561748 ( -1 , 999_999_999 , Precision :: Number ( 3 ) , "-0.000" ) ,
17571749 ( -1 , 999_999_999 , Precision :: Number ( 9 ) , "-0.000000001" ) ,
1750+ ( -1 , 999_999_999 , Precision :: Number ( 10 ) , "-0.0000000010" ) ,
17581751 ( -3 , 0 , Precision :: Number ( 3 ) , "-3.000" ) ,
17591752 ] {
17601753 assert_eq ! ( format_timestamp( seconds, nanoseconds, precision) , expected) ;
0 commit comments