@@ -504,9 +504,11 @@ impl NaiveDateTime {
504504 #[ deprecated( since = "0.4.31" , note = "use `timestamp_nanos_opt()` instead" ) ]
505505 #[ inline]
506506 #[ must_use]
507- pub fn timestamp_nanos ( & self ) -> i64 {
508- self . timestamp_nanos_opt ( )
509- . expect ( "value can not be represented in a timestamp with nanosecond precision." )
507+ pub const fn timestamp_nanos ( & self ) -> i64 {
508+ expect ! (
509+ self . timestamp_nanos_opt( ) ,
510+ "value can not be represented in a timestamp with nanosecond precision."
511+ )
510512 }
511513
512514 /// Returns the number of non-leap *nanoseconds* since midnight on January 1, 1970.
@@ -542,9 +544,9 @@ impl NaiveDateTime {
542544 /// ```
543545 #[ inline]
544546 #[ must_use]
545- pub fn timestamp_nanos_opt ( & self ) -> Option < i64 > {
547+ pub const fn timestamp_nanos_opt ( & self ) -> Option < i64 > {
546548 let mut timestamp = self . timestamp ( ) ;
547- let mut timestamp_subsec_nanos = i64 :: from ( self . timestamp_subsec_nanos ( ) ) ;
549+ let mut timestamp_subsec_nanos = self . timestamp_subsec_nanos ( ) as i64 ;
548550
549551 // subsec nanos are always non-negative, however the timestamp itself (both in seconds and in nanos) can be
550552 // negative. Now i64::MIN is NOT dividable by 1_000_000_000, so
@@ -562,7 +564,7 @@ impl NaiveDateTime {
562564 timestamp += 1 ;
563565 }
564566
565- timestamp. checked_mul ( 1_000_000_000 ) . and_then ( |ns| ns . checked_add ( timestamp_subsec_nanos) )
567+ try_opt ! ( timestamp. checked_mul( 1_000_000_000 ) ) . checked_add ( timestamp_subsec_nanos)
566568 }
567569
568570 /// Returns the number of milliseconds since the last whole non-leap second.
@@ -699,7 +701,7 @@ impl NaiveDateTime {
699701 /// Some(from_ymd(2016, 7, 9).and_hms_milli_opt(3, 5, 59, 300).unwrap()));
700702 /// ```
701703 #[ must_use]
702- pub fn checked_add_signed ( self , rhs : OldDuration ) -> Option < NaiveDateTime > {
704+ pub const fn checked_add_signed ( self , rhs : OldDuration ) -> Option < NaiveDateTime > {
703705 let ( time, rhs) = self . time . overflowing_add_signed ( rhs) ;
704706
705707 // early checking to avoid overflow in OldDuration::seconds
@@ -852,15 +854,15 @@ impl NaiveDateTime {
852854 /// Some(from_ymd(2016, 7, 7).and_hms_milli_opt(3, 6, 0, 300).unwrap()));
853855 /// ```
854856 #[ must_use]
855- pub fn checked_sub_signed ( self , rhs : OldDuration ) -> Option < NaiveDateTime > {
857+ pub const fn checked_sub_signed ( self , rhs : OldDuration ) -> Option < NaiveDateTime > {
856858 let ( time, rhs) = self . time . overflowing_sub_signed ( rhs) ;
857859
858860 // early checking to avoid overflow in OldDuration::seconds
859861 if rhs <= ( -1 << MAX_SECS_BITS ) || rhs >= ( 1 << MAX_SECS_BITS ) {
860862 return None ;
861863 }
862864
863- let date = self . date . checked_sub_signed ( OldDuration :: seconds ( rhs) ) ? ;
865+ let date = try_opt ! ( self . date. checked_sub_signed( OldDuration :: seconds( rhs) ) ) ;
864866 Some ( NaiveDateTime { date, time } )
865867 }
866868
@@ -950,7 +952,12 @@ impl NaiveDateTime {
950952 /// ```
951953 #[ must_use]
952954 pub fn signed_duration_since ( self , rhs : NaiveDateTime ) -> OldDuration {
953- self . date . signed_duration_since ( rhs. date ) + self . time . signed_duration_since ( rhs. time )
955+ expect ! (
956+ self . date
957+ . signed_duration_since( rhs. date)
958+ . checked_add( & self . time. signed_duration_since( rhs. time) ) ,
959+ "always in range"
960+ )
954961 }
955962
956963 /// Formats the combined date and time with the specified formatting items.
0 commit comments