Skip to content

Commit 5ea8d55

Browse files
committed
TimeZone trait: take NaiveDateTime by value
1 parent d286f93 commit 5ea8d55

File tree

17 files changed

+144
-152
lines changed

17 files changed

+144
-152
lines changed

bench/benches/chrono.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn bench_datetime_to_rfc2822(c: &mut Criterion) {
5050
let pst = FixedOffset::east(8 * 60 * 60).unwrap();
5151
let dt = pst
5252
.from_local_datetime(
53-
&NaiveDate::from_ymd(2018, 1, 11).unwrap().and_hms_nano(10, 5, 13, 84_660_000).unwrap(),
53+
NaiveDate::from_ymd(2018, 1, 11).unwrap().and_hms_nano(10, 5, 13, 84_660_000).unwrap(),
5454
)
5555
.unwrap();
5656
c.bench_function("bench_datetime_to_rfc2822", |b| b.iter(|| black_box(dt).to_rfc2822()));
@@ -60,7 +60,7 @@ fn bench_datetime_to_rfc3339(c: &mut Criterion) {
6060
let pst = FixedOffset::east(8 * 60 * 60).unwrap();
6161
let dt = pst
6262
.from_local_datetime(
63-
&NaiveDate::from_ymd(2018, 1, 11).unwrap().and_hms_nano(10, 5, 13, 84_660_000).unwrap(),
63+
NaiveDate::from_ymd(2018, 1, 11).unwrap().and_hms_nano(10, 5, 13, 84_660_000).unwrap(),
6464
)
6565
.unwrap();
6666
c.bench_function("bench_datetime_to_rfc3339", |b| b.iter(|| black_box(dt).to_rfc3339()));
@@ -70,7 +70,7 @@ fn bench_datetime_to_rfc3339_opts(c: &mut Criterion) {
7070
let pst = FixedOffset::east(8 * 60 * 60).unwrap();
7171
let dt = pst
7272
.from_local_datetime(
73-
&NaiveDate::from_ymd(2018, 1, 11).unwrap().and_hms_nano(10, 5, 13, 84_660_000).unwrap(),
73+
NaiveDate::from_ymd(2018, 1, 11).unwrap().and_hms_nano(10, 5, 13, 84_660_000).unwrap(),
7474
)
7575
.unwrap();
7676
c.bench_function("bench_datetime_to_rfc3339_opts", |b| {

src/datetime/mod.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
285285
#[inline]
286286
#[must_use]
287287
pub fn with_timezone<Tz2: TimeZone>(&self, tz: &Tz2) -> DateTime<Tz2> {
288-
tz.from_utc_datetime(&self.datetime)
288+
tz.from_utc_datetime(self.datetime)
289289
}
290290

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

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

357357
/// Subtracts given `Months` from the current date and time.
@@ -397,7 +397,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
397397
// range local datetime when adding `Days(0)`.
398398
let naive = self.overflowing_naive_local().checked_add_days(days).ok()?;
399399
self.timezone()
400-
.from_local_datetime(&naive)
400+
.from_local_datetime(naive)
401401
.single()
402402
.filter(|dt| dt <= &DateTime::<Utc>::MAX_UTC)
403403
}
@@ -418,7 +418,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
418418
// range local datetime when adding `Days(0)`.
419419
let naive = self.overflowing_naive_local().checked_sub_days(days).ok()?;
420420
self.timezone()
421-
.from_local_datetime(&naive)
421+
.from_local_datetime(naive)
422422
.single()
423423
.filter(|dt| dt >= &DateTime::<Utc>::MIN_UTC)
424424
}
@@ -541,7 +541,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
541541
/// let pst = FixedOffset::east(8 * 60 * 60).unwrap();
542542
/// let dt = pst
543543
/// .from_local_datetime(
544-
/// &NaiveDate::from_ymd(2018, 1, 26).unwrap().and_hms_micro(10, 30, 9, 453_829).unwrap(),
544+
/// NaiveDate::from_ymd(2018, 1, 26).unwrap().and_hms_micro(10, 30, 9, 453_829).unwrap(),
545545
/// )
546546
/// .unwrap();
547547
/// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Secs, true), "2018-01-26T10:30:09+08:00");
@@ -696,7 +696,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
696696
/// ```
697697
#[must_use]
698698
pub fn with_time(&self, time: NaiveTime) -> MappedLocalTime<Self> {
699-
self.timezone().from_local_datetime(&self.overflowing_naive_local().date().and_time(time))
699+
self.timezone().from_local_datetime(self.overflowing_naive_local().date().and_time(time))
700700
}
701701

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

920920
impl Default for DateTime<Utc> {
921921
fn default() -> Self {
922-
Utc.from_utc_datetime(&NaiveDateTime::default())
922+
Utc.from_utc_datetime(NaiveDateTime::default())
923923
}
924924
}
925925

926926
#[cfg(feature = "clock")]
927927
impl Default for DateTime<Local> {
928928
fn default() -> Self {
929-
Local.from_utc_datetime(&NaiveDateTime::default())
929+
Local.from_utc_datetime(NaiveDateTime::default())
930930
}
931931
}
932932

933933
impl Default for DateTime<FixedOffset> {
934934
fn default() -> Self {
935-
FixedOffset::west(0).unwrap().from_utc_datetime(&NaiveDateTime::default())
935+
FixedOffset::west(0).unwrap().from_utc_datetime(NaiveDateTime::default())
936936
}
937937
}
938938

@@ -1010,7 +1010,7 @@ where
10101010
F: FnMut(NaiveDateTime) -> Option<NaiveDateTime>,
10111011
{
10121012
f(dt.overflowing_naive_local())
1013-
.and_then(|datetime| dt.timezone().from_local_datetime(&datetime).single())
1013+
.and_then(|datetime| dt.timezone().from_local_datetime(datetime).single())
10141014
.filter(|dt| dt >= &DateTime::<Utc>::MIN_UTC && dt <= &DateTime::<Utc>::MAX_UTC)
10151015
}
10161016

@@ -1097,7 +1097,7 @@ impl DateTime<FixedOffset> {
10971097
/// Ok(FixedOffset::east(0)
10981098
/// .unwrap()
10991099
/// .from_local_datetime(
1100-
/// &NaiveDate::from_ymd(1983, 4, 13).unwrap().and_hms_milli(12, 9, 14, 274).unwrap()
1100+
/// NaiveDate::from_ymd(1983, 4, 13).unwrap().and_hms_milli(12, 9, 14, 274).unwrap()
11011101
/// )
11021102
/// .unwrap())
11031103
/// );
@@ -1390,7 +1390,7 @@ impl<Tz: TimeZone> AddAssign<TimeDelta> for DateTime<Tz> {
13901390
let datetime =
13911391
self.datetime.checked_add_signed(rhs).expect("`DateTime + TimeDelta` overflowed");
13921392
let tz = self.timezone();
1393-
*self = tz.from_utc_datetime(&datetime);
1393+
*self = tz.from_utc_datetime(datetime);
13941394
}
13951395
}
13961396

@@ -1510,7 +1510,7 @@ impl<Tz: TimeZone> SubAssign<TimeDelta> for DateTime<Tz> {
15101510
let datetime =
15111511
self.datetime.checked_sub_signed(rhs).expect("`DateTime - TimeDelta` overflowed");
15121512
let tz = self.timezone();
1513-
*self = tz.from_utc_datetime(&datetime)
1513+
*self = tz.from_utc_datetime(datetime)
15141514
}
15151515
}
15161516

src/datetime/serde.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,11 +1252,11 @@ mod tests {
12521252
}
12531253
fn offset_from_local_datetime(
12541254
&self,
1255-
_local: &NaiveDateTime,
1255+
_local: NaiveDateTime,
12561256
) -> MappedLocalTime<TestTimeZone> {
12571257
MappedLocalTime::Single(TestTimeZone)
12581258
}
1259-
fn offset_from_utc_datetime(&self, _utc: &NaiveDateTime) -> TestTimeZone {
1259+
fn offset_from_utc_datetime(&self, _utc: NaiveDateTime) -> TestTimeZone {
12601260
TestTimeZone
12611261
}
12621262
}

0 commit comments

Comments
 (0)