Skip to content

Commit

Permalink
Make NaiveDateTime::and_utc const
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Sep 13, 2023
1 parent 6bbbeb8 commit cdced86
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,14 @@ impl DateTime<Utc> {
pub fn from_timestamp(secs: i64, nsecs: u32) -> Option<Self> {
NaiveDateTime::from_timestamp_opt(secs, nsecs).as_ref().map(NaiveDateTime::and_utc)
}

// FIXME: remove when our MSRV is 1.61+
// This method is used by `NaiveDateTime::and_utc` because `DateTime::from_naive_utc_and_offset`
// can't be made const yet.
// Trait bounds in const function / implementation blocks were not supported until 1.61.
pub(crate) const fn from_naive_utc(datetime: NaiveDateTime) -> Self {
DateTime { datetime, offset: Utc }
}
}

impl Default for DateTime<Utc> {
Expand Down
5 changes: 3 additions & 2 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,8 +963,9 @@ impl NaiveDateTime {
/// assert_eq!(dt.timezone(), Utc);
/// ```
#[must_use]
pub fn and_utc(&self) -> DateTime<Utc> {
Utc.from_utc_datetime(self)
pub const fn and_utc(&self) -> DateTime<Utc> {
// FIXME: use `DateTime::from_naive_utc_and_offset` when our MSRV is 1.61+.
DateTime::from_naive_utc(*self)
}

/// The minimum possible `NaiveDateTime`.
Expand Down

0 comments on commit cdced86

Please sign in to comment.