7
7
use core:: borrow:: Borrow ;
8
8
use core:: cmp:: Ordering ;
9
9
use core:: ops:: { Add , AddAssign , Sub , SubAssign } ;
10
+ use core:: time:: Duration ;
10
11
use core:: { fmt, hash} ;
11
12
12
13
#[ cfg( feature = "rkyv" ) ]
@@ -18,8 +19,8 @@ use crate::format::Locale;
18
19
use crate :: format:: { DelayedFormat , Item , StrftimeItems } ;
19
20
use crate :: naive:: { IsoWeek , NaiveDate , NaiveTime } ;
20
21
use crate :: offset:: { TimeZone , Utc } ;
21
- use crate :: oldtime:: Duration as OldDuration ;
22
22
use crate :: DateTime ;
23
+ use crate :: TimeDelta ;
23
24
use crate :: { Datelike , Weekday } ;
24
25
25
26
/// ISO 8601 calendar date with time zone.
@@ -52,7 +53,7 @@ use crate::{Datelike, Weekday};
52
53
///
53
54
/// - The date is timezone-agnostic up to one day (i.e. practically always),
54
55
/// so the local date and UTC date should be equal for most cases
55
- /// even though the raw calculation between `NaiveDate` and `Duration ` may not.
56
+ /// even though the raw calculation between `NaiveDate` and `TimeDelta ` may not.
56
57
#[ derive( Clone ) ]
57
58
#[ cfg_attr( feature = "rkyv" , derive( Archive , Deserialize , Serialize ) ) ]
58
59
pub struct Date < Tz : TimeZone > {
@@ -236,31 +237,31 @@ impl<Tz: TimeZone> Date<Tz> {
236
237
tz. from_utc_date ( & self . date )
237
238
}
238
239
239
- /// Adds given `Duration ` to the current date.
240
+ /// Adds given `TimeDelta ` to the current date.
240
241
///
241
242
/// Returns `None` when it will result in overflow.
242
243
#[ inline]
243
- pub fn checked_add_signed ( self , rhs : OldDuration ) -> Option < Date < Tz > > {
244
+ pub fn checked_add_signed ( self , rhs : TimeDelta ) -> Option < Date < Tz > > {
244
245
let date = self . date . checked_add_signed ( rhs) ?;
245
246
Some ( Date { date, offset : self . offset } )
246
247
}
247
248
248
- /// Subtracts given `Duration ` from the current date.
249
+ /// Subtracts given `TimeDelta ` from the current date.
249
250
///
250
251
/// Returns `None` when it will result in overflow.
251
252
#[ inline]
252
- pub fn checked_sub_signed ( self , rhs : OldDuration ) -> Option < Date < Tz > > {
253
+ pub fn checked_sub_signed ( self , rhs : TimeDelta ) -> Option < Date < Tz > > {
253
254
let date = self . date . checked_sub_signed ( rhs) ?;
254
255
Some ( Date { date, offset : self . offset } )
255
256
}
256
257
257
258
/// Subtracts another `Date` from the current date.
258
- /// Returns a `Duration ` of integral numbers.
259
+ /// Returns a `TimeDelta ` of integral numbers.
259
260
///
260
261
/// This does not overflow or underflow at all,
261
- /// as all possible output fits in the range of `Duration `.
262
+ /// as all possible output fits in the range of `TimeDelta `.
262
263
#[ inline]
263
- pub fn signed_duration_since < Tz2 : TimeZone > ( self , rhs : Date < Tz2 > ) -> OldDuration {
264
+ pub fn signed_duration_since < Tz2 : TimeZone > ( self , rhs : Date < Tz2 > ) -> TimeDelta {
264
265
self . date . signed_duration_since ( rhs. date )
265
266
}
266
267
@@ -479,43 +480,80 @@ impl<Tz: TimeZone> hash::Hash for Date<Tz> {
479
480
}
480
481
}
481
482
482
- impl < Tz : TimeZone > Add < OldDuration > for Date < Tz > {
483
+ impl < Tz : TimeZone > Add < TimeDelta > for Date < Tz > {
483
484
type Output = Date < Tz > ;
484
485
485
486
#[ inline]
486
- fn add ( self , rhs : OldDuration ) -> Date < Tz > {
487
- self . checked_add_signed ( rhs) . expect ( "`Date + Duration` overflowed" )
487
+ fn add ( self , rhs : TimeDelta ) -> Date < Tz > {
488
+ self . checked_add_signed ( rhs) . expect ( "`Date + TimeDelta` overflowed" )
489
+ }
490
+ }
491
+ impl < Tz : TimeZone > Add < Duration > for Date < Tz > {
492
+ type Output = Date < Tz > ;
493
+
494
+ #[ inline]
495
+ fn add ( self , rhs : Duration ) -> Date < Tz > {
496
+ self . checked_add_signed ( rhs. into ( ) ) . expect ( "`Date + core::time::Duration` overflowed" )
488
497
}
489
498
}
490
499
491
- impl < Tz : TimeZone > AddAssign < OldDuration > for Date < Tz > {
500
+ impl < Tz : TimeZone > AddAssign < TimeDelta > for Date < Tz > {
492
501
#[ inline]
493
- fn add_assign ( & mut self , rhs : OldDuration ) {
494
- self . date = self . date . checked_add_signed ( rhs) . expect ( "`Date + Duration ` overflowed" ) ;
502
+ fn add_assign ( & mut self , rhs : TimeDelta ) {
503
+ self . date = self . date . checked_add_signed ( rhs) . expect ( "`Date + TimeDelta ` overflowed" ) ;
495
504
}
496
505
}
497
506
498
- impl < Tz : TimeZone > Sub < OldDuration > for Date < Tz > {
507
+ impl < Tz : TimeZone > AddAssign < Duration > for Date < Tz > {
508
+ #[ inline]
509
+ fn add_assign ( & mut self , rhs : Duration ) {
510
+ self . date = self
511
+ . date
512
+ . checked_add_signed ( rhs. into ( ) )
513
+ . expect ( "`Date + core::time::Duration` overflowed" ) ;
514
+ }
515
+ }
516
+
517
+ impl < Tz : TimeZone > Sub < TimeDelta > for Date < Tz > {
499
518
type Output = Date < Tz > ;
500
519
501
520
#[ inline]
502
- fn sub ( self , rhs : OldDuration ) -> Date < Tz > {
503
- self . checked_sub_signed ( rhs) . expect ( "`Date - Duration` overflowed" )
521
+ fn sub ( self , rhs : TimeDelta ) -> Date < Tz > {
522
+ self . checked_sub_signed ( rhs) . expect ( "`Date - TimeDelta` overflowed" )
523
+ }
524
+ }
525
+
526
+ impl < Tz : TimeZone > Sub < Duration > for Date < Tz > {
527
+ type Output = Date < Tz > ;
528
+
529
+ #[ inline]
530
+ fn sub ( self , rhs : Duration ) -> Date < Tz > {
531
+ self . checked_sub_signed ( rhs. into ( ) ) . expect ( "`Date - core::time::Duration` overflowed" )
532
+ }
533
+ }
534
+
535
+ impl < Tz : TimeZone > SubAssign < TimeDelta > for Date < Tz > {
536
+ #[ inline]
537
+ fn sub_assign ( & mut self , rhs : TimeDelta ) {
538
+ self . date = self . date . checked_sub_signed ( rhs) . expect ( "`Date - TimeDelta` overflowed" ) ;
504
539
}
505
540
}
506
541
507
- impl < Tz : TimeZone > SubAssign < OldDuration > for Date < Tz > {
542
+ impl < Tz : TimeZone > SubAssign < Duration > for Date < Tz > {
508
543
#[ inline]
509
- fn sub_assign ( & mut self , rhs : OldDuration ) {
510
- self . date = self . date . checked_sub_signed ( rhs) . expect ( "`Date - Duration` overflowed" ) ;
544
+ fn sub_assign ( & mut self , rhs : Duration ) {
545
+ self . date = self
546
+ . date
547
+ . checked_sub_signed ( rhs. into ( ) )
548
+ . expect ( "`Date - core::time::Duration` overflowed" ) ;
511
549
}
512
550
}
513
551
514
552
impl < Tz : TimeZone > Sub < Date < Tz > > for Date < Tz > {
515
- type Output = OldDuration ;
553
+ type Output = TimeDelta ;
516
554
517
555
#[ inline]
518
- fn sub ( self , rhs : Date < Tz > ) -> OldDuration {
556
+ fn sub ( self , rhs : Date < Tz > ) -> TimeDelta {
519
557
self . signed_duration_since ( rhs)
520
558
}
521
559
}
@@ -539,7 +577,7 @@ where
539
577
mod tests {
540
578
use super :: Date ;
541
579
542
- use crate :: oldtime :: Duration ;
580
+ use crate :: TimeDelta ;
543
581
use crate :: { FixedOffset , NaiveDate , Utc } ;
544
582
545
583
#[ cfg( feature = "clock" ) ]
@@ -551,15 +589,15 @@ mod tests {
551
589
const WEEKS_PER_YEAR : f32 = 52.1775 ;
552
590
553
591
// This is always at least one year because 1 year = 52.1775 weeks.
554
- let one_year_ago = Utc :: today ( ) - Duration :: weeks ( ( WEEKS_PER_YEAR * 1.5 ) . ceil ( ) as i64 ) ;
592
+ let one_year_ago = Utc :: today ( ) - TimeDelta :: weeks ( ( WEEKS_PER_YEAR * 1.5 ) . ceil ( ) as i64 ) ;
555
593
// A bit more than 2 years.
556
- let two_year_ago = Utc :: today ( ) - Duration :: weeks ( ( WEEKS_PER_YEAR * 2.5 ) . ceil ( ) as i64 ) ;
594
+ let two_year_ago = Utc :: today ( ) - TimeDelta :: weeks ( ( WEEKS_PER_YEAR * 2.5 ) . ceil ( ) as i64 ) ;
557
595
558
596
assert_eq ! ( Utc :: today( ) . years_since( one_year_ago) , Some ( 1 ) ) ;
559
597
assert_eq ! ( Utc :: today( ) . years_since( two_year_ago) , Some ( 2 ) ) ;
560
598
561
599
// If the given DateTime is later than now, the function will always return 0.
562
- let future = Utc :: today ( ) + Duration :: weeks ( 12 ) ;
600
+ let future = Utc :: today ( ) + TimeDelta :: weeks ( 12 ) ;
563
601
assert_eq ! ( Utc :: today( ) . years_since( future) , None ) ;
564
602
}
565
603
@@ -569,20 +607,20 @@ mod tests {
569
607
let date = Date :: < Utc > :: from_utc ( naivedate, Utc ) ;
570
608
let mut date_add = date;
571
609
572
- date_add += Duration :: days ( 5 ) ;
573
- assert_eq ! ( date_add, date + Duration :: days( 5 ) ) ;
610
+ date_add += TimeDelta :: days ( 5 ) ;
611
+ assert_eq ! ( date_add, date + TimeDelta :: days( 5 ) ) ;
574
612
575
613
let timezone = FixedOffset :: east ( 60 * 60 ) ;
576
614
let date = date. with_timezone ( & timezone) ;
577
615
let date_add = date_add. with_timezone ( & timezone) ;
578
616
579
- assert_eq ! ( date_add, date + Duration :: days( 5 ) ) ;
617
+ assert_eq ! ( date_add, date + TimeDelta :: days( 5 ) ) ;
580
618
581
619
let timezone = FixedOffset :: west ( 2 * 60 * 60 ) ;
582
620
let date = date. with_timezone ( & timezone) ;
583
621
let date_add = date_add. with_timezone ( & timezone) ;
584
622
585
- assert_eq ! ( date_add, date + Duration :: days( 5 ) ) ;
623
+ assert_eq ! ( date_add, date + TimeDelta :: days( 5 ) ) ;
586
624
}
587
625
588
626
#[ test]
@@ -593,8 +631,8 @@ mod tests {
593
631
let date = Local . from_utc_date ( & naivedate) ;
594
632
let mut date_add = date;
595
633
596
- date_add += Duration :: days ( 5 ) ;
597
- assert_eq ! ( date_add, date + Duration :: days( 5 ) ) ;
634
+ date_add += TimeDelta :: days ( 5 ) ;
635
+ assert_eq ! ( date_add, date + TimeDelta :: days( 5 ) ) ;
598
636
}
599
637
600
638
#[ test]
@@ -603,20 +641,20 @@ mod tests {
603
641
let date = Date :: < Utc > :: from_utc ( naivedate, Utc ) ;
604
642
let mut date_sub = date;
605
643
606
- date_sub -= Duration :: days ( 5 ) ;
607
- assert_eq ! ( date_sub, date - Duration :: days( 5 ) ) ;
644
+ date_sub -= TimeDelta :: days ( 5 ) ;
645
+ assert_eq ! ( date_sub, date - TimeDelta :: days( 5 ) ) ;
608
646
609
647
let timezone = FixedOffset :: east ( 60 * 60 ) ;
610
648
let date = date. with_timezone ( & timezone) ;
611
649
let date_sub = date_sub. with_timezone ( & timezone) ;
612
650
613
- assert_eq ! ( date_sub, date - Duration :: days( 5 ) ) ;
651
+ assert_eq ! ( date_sub, date - TimeDelta :: days( 5 ) ) ;
614
652
615
653
let timezone = FixedOffset :: west ( 2 * 60 * 60 ) ;
616
654
let date = date. with_timezone ( & timezone) ;
617
655
let date_sub = date_sub. with_timezone ( & timezone) ;
618
656
619
- assert_eq ! ( date_sub, date - Duration :: days( 5 ) ) ;
657
+ assert_eq ! ( date_sub, date - TimeDelta :: days( 5 ) ) ;
620
658
}
621
659
622
660
#[ test]
@@ -627,7 +665,7 @@ mod tests {
627
665
let date = Local . from_utc_date ( & naivedate) ;
628
666
let mut date_sub = date;
629
667
630
- date_sub -= Duration :: days ( 5 ) ;
631
- assert_eq ! ( date_sub, date - Duration :: days( 5 ) ) ;
668
+ date_sub -= TimeDelta :: days ( 5 ) ;
669
+ assert_eq ! ( date_sub, date - TimeDelta :: days( 5 ) ) ;
632
670
}
633
671
}
0 commit comments