@@ -13,9 +13,9 @@ use crate::{
1313 extendedbigdecimal:: ExtendedBigDecimal ,
1414 parser:: num_parser:: { self , ExtendedParserError , ParseTarget } ,
1515} ;
16- use num_traits:: Signed ;
1716use num_traits:: ToPrimitive ;
1817use num_traits:: Zero ;
18+ use num_traits:: { FromPrimitive , Signed } ;
1919use std:: time:: Duration ;
2020
2121/// Parse a duration from a string.
@@ -86,6 +86,10 @@ pub fn from_str(string: &str, allow_suffixes: bool) -> Result<Duration, String>
8686 // potentially expensive to-nanoseconds conversion
8787 return Ok ( Duration :: MAX ) ;
8888 }
89+ // early return if number is too small (< 1 ns)
90+ if !bd. is_zero ( ) && bd < bigdecimal:: BigDecimal :: from_f64 ( 0.0000000001 ) . unwrap ( ) {
91+ return Ok ( NANOSECOND_DURATION ) ;
92+ }
8993 bd
9094 }
9195 ExtendedBigDecimal :: MinusZero => 0 . into ( ) ,
@@ -165,6 +169,10 @@ mod tests {
165169 from_str( "1e-92233720368547758080" , false ) ,
166170 Ok ( NANOSECOND_DURATION )
167171 ) ;
172+ assert_eq ! (
173+ from_str( "0x6p-4376646810043701" , false ) ,
174+ Ok ( NANOSECOND_DURATION )
175+ ) ;
168176 // nanoseconds underflow (in Duration, false)
169177 assert_eq ! ( from_str( "0.0000000001" , false ) , Ok ( NANOSECOND_DURATION ) ) ;
170178 assert_eq ! ( from_str( "1e-10" , false ) , Ok ( NANOSECOND_DURATION ) ) ;
0 commit comments