From b49f3e4ff6bc19fa286f7db61b99aeb58519ec3a Mon Sep 17 00:00:00 2001 From: Micha White Date: Wed, 15 Jun 2022 19:07:00 -0400 Subject: [PATCH 1/7] Added a and_timezone method --- src/naive/datetime/mod.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 8b28692c42..3e2962b668 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -21,7 +21,7 @@ use crate::naive::date::{MAX_DATE, MIN_DATE}; use crate::naive::time::{MAX_TIME, MIN_TIME}; use crate::naive::{IsoWeek, NaiveDate, NaiveTime}; use crate::oldtime::Duration as OldDuration; -use crate::{Datelike, Timelike, Weekday}; +use crate::{Datelike, Timelike, Weekday, DateTime, LocalResult, TimeZone}; #[cfg(feature = "rustc-serialize")] pub(super) mod rustc_serialize; @@ -722,6 +722,19 @@ impl NaiveDateTime { pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat> { self.format_with_items(StrftimeItems::new(fmt)) } + + /// Converts the `NaiveDateTime` into the timezone-aware `DateTime` + /// with the provided timezone, if possible. + /// + /// # Example + /// + /// ``` + /// use chrono::{NaiveDate, Utc}; + /// let dt = NaiveDate::from_ymd(2015, 9, 5).and_hms(23, 56, 4).and_timezone(Utc).unwrap(); + /// assert_eq!(dt.timezone(), Utc); + pub fn and_timezone(&self, tz: Tz) -> LocalResult> { + tz.from_local_datetime(self) + } } impl Datelike for NaiveDateTime { From 17fe34a3ebf1ea43678e25a25c1438972e832d9c Mon Sep 17 00:00:00 2001 From: Micha White Date: Wed, 15 Jun 2022 19:07:11 -0400 Subject: [PATCH 2/7] Added a couple of tests --- src/naive/datetime/tests.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/naive/datetime/tests.rs b/src/naive/datetime/tests.rs index ea5f91790d..bd9afdcf16 100644 --- a/src/naive/datetime/tests.rs +++ b/src/naive/datetime/tests.rs @@ -1,7 +1,7 @@ use super::NaiveDateTime; use crate::naive::{NaiveDate, MAX_DATE, MIN_DATE}; use crate::oldtime::Duration; -use crate::Datelike; +use crate::{Datelike, Utc, FixedOffset}; use std::i64; #[test] @@ -240,3 +240,16 @@ fn test_nanosecond_range() { NaiveDateTime::from_timestamp(nanos / A_BILLION, (nanos % A_BILLION) as u32) ); } + +#[test] +fn test_and_timezone() { + let ndt = NaiveDate::from_ymd(2022, 6, 15).and_hms(18, 59, 36); + let dt_utc = ndt.and_timezone(Utc).unwrap(); + assert_eq!(dt_utc.naive_local(), ndt); + assert_eq!(dt_utc.timezone(), Utc); + + let offset_tz = FixedOffset::west(4 * 3600); + let dt_offset = ndt.and_timezone(offset_tz).unwrap(); + assert_eq!(dt_offset.naive_local(), ndt); + assert_eq!(dt_offset.timezone(), offset_tz); +} \ No newline at end of file From 78503f647dce985ba70664b58463aea559f75eac Mon Sep 17 00:00:00 2001 From: Micha White Date: Wed, 15 Jun 2022 19:08:20 -0400 Subject: [PATCH 3/7] Added the change to the changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b29520ef99..7ae344cb07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,9 +29,11 @@ Versions with only mechanical changes will be omitted from the following list. * Fix build for wasm32-unknown-emscripten (@yu-re-ka #593) * Make `ParseErrorKind` public and available through `ParseError::kind()` (#588) * Implement `DoubleEndedIterator` for `NaiveDateDaysIterator` and `NaiveDateWeeksIterator` +<<<<<<< HEAD * Fix panicking when parsing a `DateTime` (@botahamec) * Add support for getting week bounds based on a specific `NaiveDate` and a `Weekday` (#666) * Remove libc dependency from Cargo.toml. +* Add the `with_timezone` method to `NaiveDateTime` ## 0.4.19 From 61d474e3398ccd9d8706f322759b1eed27be15ca Mon Sep 17 00:00:00 2001 From: Micha White Date: Wed, 15 Jun 2022 19:09:44 -0400 Subject: [PATCH 4/7] Wrong name, silly --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ae344cb07..34734e5b1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,11 +29,10 @@ Versions with only mechanical changes will be omitted from the following list. * Fix build for wasm32-unknown-emscripten (@yu-re-ka #593) * Make `ParseErrorKind` public and available through `ParseError::kind()` (#588) * Implement `DoubleEndedIterator` for `NaiveDateDaysIterator` and `NaiveDateWeeksIterator` -<<<<<<< HEAD * Fix panicking when parsing a `DateTime` (@botahamec) * Add support for getting week bounds based on a specific `NaiveDate` and a `Weekday` (#666) * Remove libc dependency from Cargo.toml. -* Add the `with_timezone` method to `NaiveDateTime` +* Add the `and_timezone` method to `NaiveDateTime` ## 0.4.19 From 9b1dc090064244de3dacaa3e9c52978d98549ff4 Mon Sep 17 00:00:00 2001 From: Micha White Date: Wed, 15 Jun 2022 19:19:51 -0400 Subject: [PATCH 5/7] Fixed formatting --- src/naive/datetime/mod.rs | 2 +- src/naive/datetime/tests.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 3e2962b668..40ac2a78c1 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -21,7 +21,7 @@ use crate::naive::date::{MAX_DATE, MIN_DATE}; use crate::naive::time::{MAX_TIME, MIN_TIME}; use crate::naive::{IsoWeek, NaiveDate, NaiveTime}; use crate::oldtime::Duration as OldDuration; -use crate::{Datelike, Timelike, Weekday, DateTime, LocalResult, TimeZone}; +use crate::{DateTime, Datelike, LocalResult, TimeZone, Timelike, Weekday}; #[cfg(feature = "rustc-serialize")] pub(super) mod rustc_serialize; diff --git a/src/naive/datetime/tests.rs b/src/naive/datetime/tests.rs index bd9afdcf16..f9137e36b3 100644 --- a/src/naive/datetime/tests.rs +++ b/src/naive/datetime/tests.rs @@ -1,7 +1,7 @@ use super::NaiveDateTime; use crate::naive::{NaiveDate, MAX_DATE, MIN_DATE}; use crate::oldtime::Duration; -use crate::{Datelike, Utc, FixedOffset}; +use crate::{Datelike, FixedOffset, Utc}; use std::i64; #[test] @@ -252,4 +252,4 @@ fn test_and_timezone() { let dt_offset = ndt.and_timezone(offset_tz).unwrap(); assert_eq!(dt_offset.naive_local(), ndt); assert_eq!(dt_offset.timezone(), offset_tz); -} \ No newline at end of file +} From 56ebbb5e35ef7857f66d75c73001326a133618c4 Mon Sep 17 00:00:00 2001 From: Micha White Date: Sat, 18 Jun 2022 09:34:29 -0400 Subject: [PATCH 6/7] Changed the method name --- CHANGELOG.md | 2 +- src/naive/datetime/mod.rs | 2 +- src/naive/datetime/tests.rs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 34734e5b1f..dc3a65642b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,7 +32,7 @@ Versions with only mechanical changes will be omitted from the following list. * Fix panicking when parsing a `DateTime` (@botahamec) * Add support for getting week bounds based on a specific `NaiveDate` and a `Weekday` (#666) * Remove libc dependency from Cargo.toml. -* Add the `and_timezone` method to `NaiveDateTime` +* Add the `and_local_timezone` method to `NaiveDateTime` ## 0.4.19 diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 40ac2a78c1..eed08aacce 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -732,7 +732,7 @@ impl NaiveDateTime { /// use chrono::{NaiveDate, Utc}; /// let dt = NaiveDate::from_ymd(2015, 9, 5).and_hms(23, 56, 4).and_timezone(Utc).unwrap(); /// assert_eq!(dt.timezone(), Utc); - pub fn and_timezone(&self, tz: Tz) -> LocalResult> { + pub fn and_local_timezone(&self, tz: Tz) -> LocalResult> { tz.from_local_datetime(self) } } diff --git a/src/naive/datetime/tests.rs b/src/naive/datetime/tests.rs index f9137e36b3..01887332c8 100644 --- a/src/naive/datetime/tests.rs +++ b/src/naive/datetime/tests.rs @@ -244,12 +244,12 @@ fn test_nanosecond_range() { #[test] fn test_and_timezone() { let ndt = NaiveDate::from_ymd(2022, 6, 15).and_hms(18, 59, 36); - let dt_utc = ndt.and_timezone(Utc).unwrap(); + let dt_utc = ndt.and_local_timezone(Utc).unwrap(); assert_eq!(dt_utc.naive_local(), ndt); assert_eq!(dt_utc.timezone(), Utc); let offset_tz = FixedOffset::west(4 * 3600); - let dt_offset = ndt.and_timezone(offset_tz).unwrap(); + let dt_offset = ndt.and_local_timezone(offset_tz).unwrap(); assert_eq!(dt_offset.naive_local(), ndt); assert_eq!(dt_offset.timezone(), offset_tz); } From 072a011fd87c81a24a2c64571509bc1c1feb487f Mon Sep 17 00:00:00 2001 From: Micha White Date: Sat, 18 Jun 2022 09:38:51 -0400 Subject: [PATCH 7/7] Added a disclaimer to the documentation --- src/naive/datetime/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index eed08aacce..e2119e002d 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -726,11 +726,14 @@ impl NaiveDateTime { /// Converts the `NaiveDateTime` into the timezone-aware `DateTime` /// with the provided timezone, if possible. /// + /// This is experimental and might be removed in the future. Feel free to + /// let us know what you think about this API. + /// /// # Example /// /// ``` /// use chrono::{NaiveDate, Utc}; - /// let dt = NaiveDate::from_ymd(2015, 9, 5).and_hms(23, 56, 4).and_timezone(Utc).unwrap(); + /// let dt = NaiveDate::from_ymd(2015, 9, 5).and_hms(23, 56, 4).and_local_timezone(Utc).unwrap(); /// assert_eq!(dt.timezone(), Utc); pub fn and_local_timezone(&self, tz: Tz) -> LocalResult> { tz.from_local_datetime(self)