mirror of
https://github.com/chronotope/chrono.git
synced 2025-10-02 15:26:12 +00:00
Make NaiveDateTime::and_utc
const
This commit is contained in:
parent
a0d3800412
commit
fe4713a4f0
@ -622,6 +622,14 @@ impl DateTime<Utc> {
|
||||
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 }
|
||||
}
|
||||
|
||||
/// The Unix Epoch, 1970-01-01 00:00:00 UTC.
|
||||
pub const UNIX_EPOCH: Self = Self { datetime: NaiveDateTime::UNIX_EPOCH, offset: Utc };
|
||||
}
|
||||
|
@ -1030,8 +1030,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`.
|
||||
|
Loading…
x
Reference in New Issue
Block a user