Skip to content

Commit

Permalink
Use NaiveDateTime::and_local_timezone in `TimeZone::from_local_date…
Browse files Browse the repository at this point in the history
…time`
  • Loading branch information
pitdicker committed Aug 6, 2023
1 parent 36602d3 commit bc10f89
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,14 @@ impl NaiveDateTime {
/// ```
#[must_use]
pub fn and_local_timezone<Tz: TimeZone>(&self, tz: Tz) -> LocalResult<DateTime<Tz>> {
self.and_local_timezone_internal(&tz)
}

// FIXME: ideally tz should be taken by reference, but that would be a breaking change.
pub(crate) fn and_local_timezone_internal<Tz: TimeZone>(
&self,
tz: &Tz,
) -> LocalResult<DateTime<Tz>> {
let local_result_offset = tz.offset_from_local_datetime(self);
local_time_min_offset(local_result_offset, self).unwrap_or(LocalResult::None)
}
Expand Down
29 changes: 27 additions & 2 deletions src/offset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,7 @@ pub trait TimeZone: Sized + Clone {
/// Converts the local `NaiveDateTime` to the timezone-aware `DateTime` if possible.
#[allow(clippy::wrong_self_convention)]
fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Self>> {
self.offset_from_local_datetime(local)
.map(|offset| DateTime::from_naive_utc_and_offset(*local - offset.fix(), offset))
local.and_local_timezone_internal(self)
}

/// Creates the offset for given UTC `NaiveDate`. This cannot fail.
Expand Down Expand Up @@ -504,6 +503,32 @@ pub trait TimeZone: Sized + Clone {
mod tests {
use super::*;

#[test]
fn test_fixed_offset_min_max_dates() {
for offset_hour in -23..=23 {
dbg!(offset_hour);
let offset = FixedOffset::east_opt(offset_hour * 60 * 60).unwrap();

let local_max = offset.from_utc_datetime(&NaiveDateTime::MAX);
assert_eq!(local_max.naive_utc(), NaiveDateTime::MAX);
let local_min = offset.from_utc_datetime(&NaiveDateTime::MIN);
assert_eq!(local_min.naive_utc(), NaiveDateTime::MIN);

let local_max = offset.from_local_datetime(&NaiveDateTime::MAX);
if offset_hour >= 0 {
assert_eq!(local_max.unwrap().naive_local(), NaiveDateTime::MAX);
} else {
assert_eq!(local_max, LocalResult::None);
}
let local_min = offset.from_local_datetime(&NaiveDateTime::MIN);
if offset_hour <= 0 {
assert_eq!(local_min.unwrap().naive_local(), NaiveDateTime::MIN);
} else {
assert_eq!(local_min, LocalResult::None);
}
}
}

#[test]
fn test_negative_millis() {
let dt = Utc.timestamp_millis_opt(-1000).unwrap();
Expand Down

0 comments on commit bc10f89

Please sign in to comment.