Skip to content

Commit

Permalink
TimeZone trait: take NaiveDateTime by value
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 18, 2024
1 parent f1c72a5 commit 18b96a1
Show file tree
Hide file tree
Showing 18 changed files with 157 additions and 165 deletions.
6 changes: 3 additions & 3 deletions bench/benches/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn bench_datetime_to_rfc2822(c: &mut Criterion) {
let pst = FixedOffset::east(8 * 60 * 60).unwrap();
let dt = pst
.from_local_datetime(
&NaiveDate::from_ymd(2018, 1, 11).unwrap().and_hms_nano(10, 5, 13, 84_660_000).unwrap(),
NaiveDate::from_ymd(2018, 1, 11).unwrap().and_hms_nano(10, 5, 13, 84_660_000).unwrap(),
)
.unwrap();
c.bench_function("bench_datetime_to_rfc2822", |b| b.iter(|| black_box(dt).to_rfc2822()));
Expand All @@ -60,7 +60,7 @@ fn bench_datetime_to_rfc3339(c: &mut Criterion) {
let pst = FixedOffset::east(8 * 60 * 60).unwrap();
let dt = pst
.from_local_datetime(
&NaiveDate::from_ymd(2018, 1, 11).unwrap().and_hms_nano(10, 5, 13, 84_660_000).unwrap(),
NaiveDate::from_ymd(2018, 1, 11).unwrap().and_hms_nano(10, 5, 13, 84_660_000).unwrap(),
)
.unwrap();
c.bench_function("bench_datetime_to_rfc3339", |b| b.iter(|| black_box(dt).to_rfc3339()));
Expand All @@ -70,7 +70,7 @@ fn bench_datetime_to_rfc3339_opts(c: &mut Criterion) {
let pst = FixedOffset::east(8 * 60 * 60).unwrap();
let dt = pst
.from_local_datetime(
&NaiveDate::from_ymd(2018, 1, 11).unwrap().and_hms_nano(10, 5, 13, 84_660_000).unwrap(),
NaiveDate::from_ymd(2018, 1, 11).unwrap().and_hms_nano(10, 5, 13, 84_660_000).unwrap(),
)
.unwrap();
c.bench_function("bench_datetime_to_rfc3339_opts", |b| {
Expand Down
28 changes: 14 additions & 14 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
#[inline]
#[must_use]
pub fn with_timezone<Tz2: TimeZone>(&self, tz: &Tz2) -> DateTime<Tz2> {
tz.from_utc_datetime(&self.datetime)
tz.from_utc_datetime(self.datetime)
}

/// Fix the offset from UTC to its current value, dropping the associated timezone information.
Expand Down Expand Up @@ -314,7 +314,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
pub fn checked_add_signed(self, rhs: TimeDelta) -> Option<DateTime<Tz>> {
let datetime = self.datetime.checked_add_signed(rhs).ok()?;
let tz = self.timezone();
Some(tz.from_utc_datetime(&datetime))
Some(tz.from_utc_datetime(datetime))
}

/// Adds given `Months` to the current date and time.
Expand Down Expand Up @@ -351,7 +351,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
pub fn checked_sub_signed(self, rhs: TimeDelta) -> Option<DateTime<Tz>> {
let datetime = self.datetime.checked_sub_signed(rhs).ok()?;
let tz = self.timezone();
Some(tz.from_utc_datetime(&datetime))
Some(tz.from_utc_datetime(datetime))
}

/// Subtracts given `Months` from the current date and time.
Expand Down Expand Up @@ -397,7 +397,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
// range local datetime when adding `Days(0)`.
let naive = self.overflowing_naive_local().checked_add_days(days).ok()?;
self.timezone()
.from_local_datetime(&naive)
.from_local_datetime(naive)
.single()
.filter(|dt| dt <= &DateTime::<Utc>::MAX_UTC)
}
Expand All @@ -418,7 +418,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
// range local datetime when adding `Days(0)`.
let naive = self.overflowing_naive_local().checked_sub_days(days).ok()?;
self.timezone()
.from_local_datetime(&naive)
.from_local_datetime(naive)
.single()
.filter(|dt| dt >= &DateTime::<Utc>::MIN_UTC)
}
Expand Down Expand Up @@ -541,7 +541,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// let pst = FixedOffset::east(8 * 60 * 60).unwrap();
/// let dt = pst
/// .from_local_datetime(
/// &NaiveDate::from_ymd(2018, 1, 26).unwrap().and_hms_micro(10, 30, 9, 453_829).unwrap(),
/// NaiveDate::from_ymd(2018, 1, 26).unwrap().and_hms_micro(10, 30, 9, 453_829).unwrap(),
/// )
/// .unwrap();
/// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Secs, true), "2018-01-26T10:30:09+08:00");
Expand Down Expand Up @@ -696,7 +696,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// ```
#[must_use]
pub fn with_time(&self, time: NaiveTime) -> MappedLocalTime<Self> {
self.timezone().from_local_datetime(&self.overflowing_naive_local().date().and_time(time))
self.timezone().from_local_datetime(self.overflowing_naive_local().date().and_time(time))
}

/// Makes a new `DateTime` with the hour number changed.
Expand Down Expand Up @@ -919,20 +919,20 @@ impl DateTime<Utc> {

impl Default for DateTime<Utc> {
fn default() -> Self {
Utc.from_utc_datetime(&NaiveDateTime::default())
Utc.from_utc_datetime(NaiveDateTime::default())
}
}

#[cfg(feature = "clock")]
impl Default for DateTime<Local> {
fn default() -> Self {
Local.from_utc_datetime(&NaiveDateTime::default())
Local.from_utc_datetime(NaiveDateTime::default())
}
}

impl Default for DateTime<FixedOffset> {
fn default() -> Self {
FixedOffset::west(0).unwrap().from_utc_datetime(&NaiveDateTime::default())
FixedOffset::west(0).unwrap().from_utc_datetime(NaiveDateTime::default())
}
}

Expand Down Expand Up @@ -1010,7 +1010,7 @@ where
F: FnMut(NaiveDateTime) -> Option<NaiveDateTime>,
{
f(dt.overflowing_naive_local())
.and_then(|datetime| dt.timezone().from_local_datetime(&datetime).single())
.and_then(|datetime| dt.timezone().from_local_datetime(datetime).single())
.filter(|dt| dt >= &DateTime::<Utc>::MIN_UTC && dt <= &DateTime::<Utc>::MAX_UTC)
}

Expand Down Expand Up @@ -1097,7 +1097,7 @@ impl DateTime<FixedOffset> {
/// Ok(FixedOffset::east(0)
/// .unwrap()
/// .from_local_datetime(
/// &NaiveDate::from_ymd(1983, 4, 13).unwrap().and_hms_milli(12, 9, 14, 274).unwrap()
/// NaiveDate::from_ymd(1983, 4, 13).unwrap().and_hms_milli(12, 9, 14, 274).unwrap()
/// )
/// .unwrap())
/// );
Expand Down Expand Up @@ -1390,7 +1390,7 @@ impl<Tz: TimeZone> AddAssign<TimeDelta> for DateTime<Tz> {
let datetime =
self.datetime.checked_add_signed(rhs).expect("`DateTime + TimeDelta` overflowed");
let tz = self.timezone();
*self = tz.from_utc_datetime(&datetime);
*self = tz.from_utc_datetime(datetime);
}
}

Expand Down Expand Up @@ -1510,7 +1510,7 @@ impl<Tz: TimeZone> SubAssign<TimeDelta> for DateTime<Tz> {
let datetime =
self.datetime.checked_sub_signed(rhs).expect("`DateTime - TimeDelta` overflowed");
let tz = self.timezone();
*self = tz.from_utc_datetime(&datetime)
*self = tz.from_utc_datetime(datetime)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/datetime/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,11 +1252,11 @@ mod tests {
}
fn offset_from_local_datetime(
&self,
_local: &NaiveDateTime,
_local: NaiveDateTime,
) -> MappedLocalTime<TestTimeZone> {
MappedLocalTime::Single(TestTimeZone)
}
fn offset_from_utc_datetime(&self, _utc: &NaiveDateTime) -> TestTimeZone {
fn offset_from_utc_datetime(&self, _utc: NaiveDateTime) -> TestTimeZone {
TestTimeZone
}
}
Expand Down
Loading

0 comments on commit 18b96a1

Please sign in to comment.