@@ -1437,6 +1437,65 @@ fn test_min_max_setters() {
1437
1437
assert_eq ! ( beyond_max. with_nanosecond( beyond_max. nanosecond( ) ) , Some ( beyond_max) ) ;
1438
1438
}
1439
1439
1440
+ #[ test]
1441
+ fn test_min_max_complex ( ) {
1442
+ let offset_min = FixedOffset :: west_opt ( 2 * 60 * 60 ) . unwrap ( ) ;
1443
+ let beyond_min = offset_min. from_utc_datetime ( & NaiveDateTime :: MIN ) ;
1444
+ let offset_max = FixedOffset :: east_opt ( 2 * 60 * 60 ) . unwrap ( ) ;
1445
+ let beyond_max = offset_max. from_utc_datetime ( & NaiveDateTime :: MAX ) ;
1446
+ let max_time = NaiveTime :: from_hms_nano_opt ( 23 , 59 , 59 , 999_999_999 ) . unwrap ( ) ;
1447
+
1448
+ assert_eq ! ( beyond_min. checked_add_days( Days :: new( 0 ) ) , Some ( beyond_min) ) ;
1449
+ assert_eq ! (
1450
+ beyond_min. checked_add_days( Days :: new( 1 ) ) ,
1451
+ Some ( offset_min. from_utc_datetime( & ( NaiveDate :: MIN + Days ( 1 ) ) . and_time( NaiveTime :: MIN ) ) )
1452
+ ) ;
1453
+ assert_eq ! ( beyond_min. checked_sub_days( Days :: new( 0 ) ) , Some ( beyond_min) ) ;
1454
+ assert_eq ! ( beyond_min. checked_sub_days( Days :: new( 1 ) ) , None ) ;
1455
+ assert_eq ! ( beyond_min. checked_add_months( Months :: new( 0 ) ) , Some ( beyond_min) ) ;
1456
+ assert_eq ! (
1457
+ beyond_min. checked_add_months( Months :: new( 1 ) ) ,
1458
+ Some ( offset_min. from_utc_datetime( & ( NaiveDate :: MIN + Months ( 1 ) ) . and_time( NaiveTime :: MIN ) ) )
1459
+ ) ;
1460
+ assert_eq ! ( beyond_min. checked_sub_months( Months :: new( 0 ) ) , Some ( beyond_min) ) ;
1461
+ assert_eq ! ( beyond_min. checked_sub_months( Months :: new( 1 ) ) , None ) ;
1462
+ assert_eq ! ( beyond_min. with_year( beyond_min. year( ) ) , Some ( beyond_min) ) ;
1463
+ let res = NaiveDate :: MIN . with_year ( 2021 ) . unwrap ( ) . and_time ( NaiveTime :: MIN ) + offset_min;
1464
+ assert_eq ! ( beyond_min. with_year( 2020 ) , offset_min. from_local_datetime( & res) . single( ) ) ;
1465
+ assert_eq ! (
1466
+ offset_min
1467
+ . from_utc_datetime(
1468
+ & NaiveDate :: from_ymd_opt( 2023 , 1 , 1 ) . unwrap( ) . and_time( NaiveTime :: MIN )
1469
+ )
1470
+ . with_year( NaiveDate :: MIN . year( ) - 1 ) ,
1471
+ Some ( beyond_min)
1472
+ ) ;
1473
+
1474
+ assert_eq ! ( beyond_max. checked_add_days( Days :: new( 0 ) ) , Some ( beyond_max) ) ;
1475
+ assert_eq ! ( beyond_max. checked_add_days( Days :: new( 1 ) ) , None ) ;
1476
+ assert_eq ! ( beyond_max. checked_sub_days( Days :: new( 0 ) ) , Some ( beyond_max) ) ;
1477
+ assert_eq ! (
1478
+ beyond_max. checked_sub_days( Days :: new( 1 ) ) ,
1479
+ Some ( offset_max. from_utc_datetime( & ( NaiveDate :: MAX - Days ( 1 ) ) . and_time( max_time) ) )
1480
+ ) ;
1481
+ assert_eq ! ( beyond_max. checked_add_months( Months :: new( 0 ) ) , Some ( beyond_max) ) ;
1482
+ assert_eq ! ( beyond_max. checked_add_months( Months :: new( 1 ) ) , None ) ;
1483
+ assert_eq ! ( beyond_max. checked_sub_months( Months :: new( 0 ) ) , Some ( beyond_max) ) ;
1484
+ assert_eq ! (
1485
+ beyond_max. checked_sub_months( Months :: new( 1 ) ) ,
1486
+ Some ( offset_max. from_utc_datetime( & ( NaiveDate :: MAX - Months ( 1 ) ) . and_time( max_time) ) )
1487
+ ) ;
1488
+ assert_eq ! ( beyond_max. with_year( beyond_max. year( ) ) , Some ( beyond_max) ) ;
1489
+ let res = NaiveDate :: MAX . with_year ( 2019 ) . unwrap ( ) . and_time ( max_time) + offset_max;
1490
+ assert_eq ! ( beyond_max. with_year( 2020 ) , offset_max. from_local_datetime( & res) . single( ) ) ;
1491
+ assert_eq ! (
1492
+ offset_max
1493
+ . from_utc_datetime( & NaiveDate :: from_ymd_opt( 2023 , 12 , 31 ) . unwrap( ) . and_time( max_time) )
1494
+ . with_year( NaiveDate :: MAX . year( ) + 1 ) ,
1495
+ Some ( beyond_max)
1496
+ ) ;
1497
+ }
1498
+
1440
1499
#[ test]
1441
1500
#[ should_panic]
1442
1501
fn test_local_beyond_min_datetime ( ) {
0 commit comments