Skip to content

Commit 1990ffd

Browse files
committed
change NaiveDateTime and DateTime<Tz> to use Duration
1 parent d086ca9 commit 1990ffd

File tree

11 files changed

+1611
-159
lines changed

11 files changed

+1611
-159
lines changed

src/datetime/mod.rs

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use core::borrow::Borrow;
1313
use core::cmp::Ordering;
1414
use core::fmt::Write;
1515
use core::ops::{Add, AddAssign, Sub, SubAssign};
16+
use core::time::Duration;
1617
use core::{fmt, hash, str};
1718
#[cfg(feature = "std")]
1819
use std::string::ToString;
@@ -314,12 +315,24 @@ impl<Tz: TimeZone> DateTime<Tz> {
314315
///
315316
/// Returns `None` when it will result in overflow.
316317
#[inline]
318+
#[deprecated(since = "0.4.24", note = "Use checked_add() instead")]
319+
#[allow(deprecated)]
317320
pub fn checked_add_signed(self, rhs: OldDuration) -> Option<DateTime<Tz>> {
318321
let datetime = self.datetime.checked_add_signed(rhs)?;
319322
let tz = self.timezone();
320323
Some(tz.from_utc_datetime(&datetime))
321324
}
322325

326+
/// Adds given `Duration` to the current date and time.
327+
///
328+
/// Returns `None` when it will result in overflow.
329+
#[inline]
330+
pub fn checked_add(self, rhs: Duration) -> Option<DateTime<Tz>> {
331+
let datetime = self.datetime.checked_add(rhs)?;
332+
let tz = self.timezone();
333+
Some(tz.from_utc_datetime(&datetime))
334+
}
335+
323336
/// Adds given `Months` to the current date and time.
324337
///
325338
/// Returns `None` when it will result in overflow, or if the
@@ -337,12 +350,24 @@ impl<Tz: TimeZone> DateTime<Tz> {
337350
///
338351
/// Returns `None` when it will result in overflow.
339352
#[inline]
353+
#[deprecated(since = "0.4.24", note = "Use checked_sub() instead")]
354+
#[allow(deprecated)]
340355
pub fn checked_sub_signed(self, rhs: OldDuration) -> Option<DateTime<Tz>> {
341356
let datetime = self.datetime.checked_sub_signed(rhs)?;
342357
let tz = self.timezone();
343358
Some(tz.from_utc_datetime(&datetime))
344359
}
345360

361+
/// Subtracts given `Duration` from the current date and time.
362+
///
363+
/// Returns `None` when it will result in overflow.
364+
#[inline]
365+
pub fn checked_sub(self, rhs: Duration) -> Option<DateTime<Tz>> {
366+
let datetime = self.datetime.checked_sub(rhs)?;
367+
let tz = self.timezone();
368+
Some(tz.from_utc_datetime(&datetime))
369+
}
370+
346371
/// Subtracts given `Months` from the current date and time.
347372
///
348373
/// Returns `None` when it will result in overflow, or if the
@@ -379,10 +404,29 @@ impl<Tz: TimeZone> DateTime<Tz> {
379404
/// Subtracts another `DateTime` from the current date and time.
380405
/// This does not overflow or underflow at all.
381406
#[inline]
407+
#[deprecated(since = "0.4.24", note = "Use checked_duration_since() or abs_diff() instead")]
408+
#[allow(deprecated)]
382409
pub fn signed_duration_since<Tz2: TimeZone>(self, rhs: DateTime<Tz2>) -> OldDuration {
383410
self.datetime.signed_duration_since(rhs.datetime)
384411
}
385412

413+
/// Subtracts another `DateTime` from the current date and time.
414+
/// This does not overflow or underflow at all.
415+
#[inline]
416+
pub fn checked_duration_since<Tz2: TimeZone>(
417+
self,
418+
rhs: DateTime<Tz2>,
419+
) -> Result<Duration, Duration> {
420+
self.datetime.checked_duration_since(rhs.datetime)
421+
}
422+
423+
/// Subtracts another `DateTime` from the current date and time.
424+
/// This does not overflow or underflow at all.
425+
#[inline]
426+
pub fn abs_diff<Tz2: TimeZone>(self, rhs: DateTime<Tz2>) -> Duration {
427+
self.datetime.abs_diff(rhs.datetime)
428+
}
429+
386430
/// Returns a view to the naive UTC datetime.
387431
#[inline]
388432
pub fn naive_utc(&self) -> NaiveDateTime {
@@ -903,6 +947,7 @@ impl<Tz: TimeZone> hash::Hash for DateTime<Tz> {
903947
}
904948
}
905949

950+
#[allow(deprecated)]
906951
impl<Tz: TimeZone> Add<OldDuration> for DateTime<Tz> {
907952
type Output = DateTime<Tz>;
908953

@@ -912,6 +957,7 @@ impl<Tz: TimeZone> Add<OldDuration> for DateTime<Tz> {
912957
}
913958
}
914959

960+
#[allow(deprecated)]
915961
impl<Tz: TimeZone> AddAssign<OldDuration> for DateTime<Tz> {
916962
#[inline]
917963
fn add_assign(&mut self, rhs: OldDuration) {
@@ -922,6 +968,24 @@ impl<Tz: TimeZone> AddAssign<OldDuration> for DateTime<Tz> {
922968
}
923969
}
924970

971+
impl<Tz: TimeZone> Add<Duration> for DateTime<Tz> {
972+
type Output = DateTime<Tz>;
973+
974+
#[inline]
975+
fn add(self, rhs: Duration) -> DateTime<Tz> {
976+
self.checked_add(rhs).expect("`DateTime + Duration` overflowed")
977+
}
978+
}
979+
980+
impl<Tz: TimeZone> AddAssign<Duration> for DateTime<Tz> {
981+
#[inline]
982+
fn add_assign(&mut self, rhs: Duration) {
983+
let datetime = self.datetime.checked_add(rhs).expect("`DateTime + Duration` overflowed");
984+
let tz = self.timezone();
985+
*self = tz.from_utc_datetime(&datetime);
986+
}
987+
}
988+
925989
impl<Tz: TimeZone> Add<Months> for DateTime<Tz> {
926990
type Output = DateTime<Tz>;
927991

@@ -930,6 +994,7 @@ impl<Tz: TimeZone> Add<Months> for DateTime<Tz> {
930994
}
931995
}
932996

997+
#[allow(deprecated)]
933998
impl<Tz: TimeZone> Sub<OldDuration> for DateTime<Tz> {
934999
type Output = DateTime<Tz>;
9351000

@@ -939,6 +1004,7 @@ impl<Tz: TimeZone> Sub<OldDuration> for DateTime<Tz> {
9391004
}
9401005
}
9411006

1007+
#[allow(deprecated)]
9421008
impl<Tz: TimeZone> SubAssign<OldDuration> for DateTime<Tz> {
9431009
#[inline]
9441010
fn sub_assign(&mut self, rhs: OldDuration) {
@@ -949,6 +1015,24 @@ impl<Tz: TimeZone> SubAssign<OldDuration> for DateTime<Tz> {
9491015
}
9501016
}
9511017

1018+
impl<Tz: TimeZone> Sub<Duration> for DateTime<Tz> {
1019+
type Output = DateTime<Tz>;
1020+
1021+
#[inline]
1022+
fn sub(self, rhs: Duration) -> DateTime<Tz> {
1023+
self.checked_sub(rhs).expect("`DateTime - Duration` overflowed")
1024+
}
1025+
}
1026+
1027+
impl<Tz: TimeZone> SubAssign<Duration> for DateTime<Tz> {
1028+
#[inline]
1029+
fn sub_assign(&mut self, rhs: Duration) {
1030+
let datetime = self.datetime.checked_sub(rhs).expect("`DateTime - Duration` overflowed");
1031+
let tz = self.timezone();
1032+
*self = tz.from_utc_datetime(&datetime)
1033+
}
1034+
}
1035+
9521036
impl<Tz: TimeZone> Sub<Months> for DateTime<Tz> {
9531037
type Output = DateTime<Tz>;
9541038

@@ -957,6 +1041,7 @@ impl<Tz: TimeZone> Sub<Months> for DateTime<Tz> {
9571041
}
9581042
}
9591043

1044+
#[allow(deprecated)]
9601045
impl<Tz: TimeZone> Sub<DateTime<Tz>> for DateTime<Tz> {
9611046
type Output = OldDuration;
9621047

@@ -1073,8 +1158,6 @@ impl From<SystemTime> for DateTime<Local> {
10731158
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
10741159
impl<Tz: TimeZone> From<DateTime<Tz>> for SystemTime {
10751160
fn from(dt: DateTime<Tz>) -> SystemTime {
1076-
use std::time::Duration;
1077-
10781161
let sec = dt.timestamp();
10791162
let nsec = dt.timestamp_subsec_nanos();
10801163
if sec < 0 {

0 commit comments

Comments
 (0)