Skip to content

Commit

Permalink
NaiveDateTime to DateTime helper method (#711)
Browse files Browse the repository at this point in the history
* Added a and_timezone method

* Added a couple of tests

* Added the change to the changelog

* Wrong name, silly

* Fixed formatting

* Changed the method name

* Added a disclaimer to the documentation
  • Loading branch information
botahamec committed Jun 19, 2022
1 parent 13e1d48 commit cb92398
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -32,6 +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_local_timezone` method to `NaiveDateTime`

## 0.4.19

Expand Down
18 changes: 17 additions & 1 deletion src/naive/datetime/mod.rs
Expand Up @@ -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::{DateTime, Datelike, LocalResult, TimeZone, Timelike, Weekday};

#[cfg(feature = "rustc-serialize")]
pub(super) mod rustc_serialize;
Expand Down Expand Up @@ -722,6 +722,22 @@ impl NaiveDateTime {
pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>> {
self.format_with_items(StrftimeItems::new(fmt))
}

/// Converts the `NaiveDateTime` into the timezone-aware `DateTime<Tz>`
/// 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_local_timezone(Utc).unwrap();
/// assert_eq!(dt.timezone(), Utc);
pub fn and_local_timezone<Tz: TimeZone>(&self, tz: Tz) -> LocalResult<DateTime<Tz>> {
tz.from_local_datetime(self)
}
}

impl Datelike for NaiveDateTime {
Expand Down
15 changes: 14 additions & 1 deletion 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, FixedOffset, Utc};
use std::i64;

#[test]
Expand Down Expand Up @@ -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_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_local_timezone(offset_tz).unwrap();
assert_eq!(dt_offset.naive_local(), ndt);
assert_eq!(dt_offset.timezone(), offset_tz);
}

0 comments on commit cb92398

Please sign in to comment.