From b35094512d829eb6b470efeed58dd36c2a6a4260 Mon Sep 17 00:00:00 2001 From: Kurtis Nusbaum Date: Tue, 31 Jan 2023 00:08:07 +0000 Subject: [PATCH] add and_utc_timezone function --- AUTHORS.txt | 1 + src/naive/datetime/mod.rs | 24 +++++++++++++++++++++--- src/naive/datetime/tests.rs | 10 +++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/AUTHORS.txt b/AUTHORS.txt index caff57915c..e0239ea92c 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -31,6 +31,7 @@ John Nagle Jonas mg János Illés Ken Tossell +Kurtis Nusbaum Martin Risell Lilja Richard Petrie Ryan Lewis diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index ab9fda089d..477f8d51a0 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -18,6 +18,7 @@ use crate::format::DelayedFormat; use crate::format::{parse, ParseError, ParseResult, Parsed, StrftimeItems}; use crate::format::{Fixed, Item, Numeric, Pad}; use crate::naive::{Days, IsoWeek, NaiveDate, NaiveTime}; +use crate::offset::Utc; use crate::oldtime::Duration as OldDuration; use crate::{DateTime, Datelike, LocalResult, Months, TimeZone, Timelike, Weekday}; @@ -887,14 +888,31 @@ impl NaiveDateTime { /// # Example /// /// ``` - /// use chrono::{NaiveDate, Utc}; - /// let dt = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap().and_hms_opt(23, 56, 4).unwrap().and_local_timezone(Utc).unwrap(); - /// assert_eq!(dt.timezone(), Utc); + /// use chrono::{NaiveDate, FixedOffset}; + /// let hour = 3600; + /// let tz = FixedOffset::east_opt(5 * hour).unwrap(); + /// let dt = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap().and_hms_opt(23, 56, 4).unwrap().and_local_timezone(tz).unwrap(); + /// assert_eq!(dt.timezone(), tz); + /// ``` #[must_use] pub fn and_local_timezone(&self, tz: Tz) -> LocalResult> { tz.from_local_datetime(self) } + /// Converts the `NaiveDateTime` into the timezone-aware `DateTime`. + /// + /// # Example + /// + /// ``` + /// use chrono::{NaiveDate, NaiveTime, Utc}; + /// let dt = NaiveDate::from_ymd_opt(2023, 1, 30).unwrap().and_hms_opt(19, 32, 33).unwrap().and_utc(); + /// assert_eq!(dt.timezone(), Utc); + /// ``` + #[must_use] + pub fn and_utc(&self) -> DateTime { + Utc.from_utc_datetime(self) + } + /// The minimum possible `NaiveDateTime`. pub const MIN: Self = Self { date: NaiveDate::MIN, time: NaiveTime::MIN }; /// The maximum possible `NaiveDateTime`. diff --git a/src/naive/datetime/tests.rs b/src/naive/datetime/tests.rs index 810da47015..96ff3b15b5 100644 --- a/src/naive/datetime/tests.rs +++ b/src/naive/datetime/tests.rs @@ -370,7 +370,7 @@ fn test_nanosecond_range() { } #[test] -fn test_and_timezone() { +fn test_and_local_timezone() { let ndt = NaiveDate::from_ymd_opt(2022, 6, 15).unwrap().and_hms_opt(18, 59, 36).unwrap(); let dt_utc = ndt.and_local_timezone(Utc).unwrap(); assert_eq!(dt_utc.naive_local(), ndt); @@ -381,3 +381,11 @@ fn test_and_timezone() { assert_eq!(dt_offset.naive_local(), ndt); assert_eq!(dt_offset.timezone(), offset_tz); } + +#[test] +fn test_and_utc() { + let ndt = NaiveDate::from_ymd_opt(2023, 1, 30).unwrap().and_hms_opt(19, 32, 33).unwrap(); + let dt_utc = ndt.and_utc(); + assert_eq!(dt_utc.naive_local(), ndt); + assert_eq!(dt_utc.timezone(), Utc); +}