Skip to content

Commit

Permalink
add and_utc_timezone function
Browse files Browse the repository at this point in the history
  • Loading branch information
Kurtis Nusbaum authored and djc committed May 19, 2023
1 parent ada0629 commit 7628595
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS.txt
Expand Up @@ -31,6 +31,7 @@ John Nagle <nagle@sitetruth.com>
Jonas mg <jonasmg@yepmail.net>
János Illés <ijanos@gmail.com>
Ken Tossell <ken@tossell.net>
Kurtis Nusbaum <kcommiter@gmail.com>
Martin Risell Lilja <martin.risell.lilja@gmail.com>
Richard Petrie <rap1011@ksu.edu>
Ryan Lewis <ryansname@gmail.com>
Expand Down
24 changes: 21 additions & 3 deletions src/naive/datetime/mod.rs
Expand Up @@ -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};

Expand Down Expand Up @@ -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<Tz: TimeZone>(&self, tz: Tz) -> LocalResult<DateTime<Tz>> {
tz.from_local_datetime(self)
}

/// Converts the `NaiveDateTime` into the timezone-aware `DateTime<Utc>`.
///
/// # 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> {
Utc.from_utc_datetime(self)
}

/// The minimum possible `NaiveDateTime`.
pub const MIN: Self = Self { date: NaiveDate::MIN, time: NaiveTime::MIN };
/// The maximum possible `NaiveDateTime`.
Expand Down
10 changes: 9 additions & 1 deletion src/naive/datetime/tests.rs
Expand Up @@ -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);
Expand All @@ -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);
}

0 comments on commit 7628595

Please sign in to comment.