Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make methods on NaiveDateTime const where possible #1286

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Expand Down
23 changes: 23 additions & 0 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,13 @@ impl NaiveDate {
self.ymdf >> 13
}

/// Returns the day of year starting from 1.
// This duplicates `Datelike::ordinal()`, because trait methods can't be const yet.
#[inline]
const fn ordinal(&self) -> u32 {
self.of().ordinal()
}

// This duplicates `Datelike::month()`, because trait methods can't be const yet.
#[inline]
const fn month(&self) -> u32 {
Expand All @@ -1455,6 +1462,22 @@ impl NaiveDate {
self.of().weekday()
}

/// Counts the days in the proleptic Gregorian calendar, with January 1, Year 1 (CE) as day 1.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a fun recommendation: link to the meaning of "AD 1":

 /// ... with January 1, Year 1 (CE) as day 1, i.e. [AD 1](https://en.wikipedia.org/wiki/AD_1)

// This duplicates `Datelike::num_days_from_ce()`, because trait methods can't be const yet.
pub(crate) const fn num_days_from_ce(&self) -> i32 {
// we know this wouldn't overflow since year is limited to 1/2^13 of i32's full range.
let mut year = self.year() - 1;
let mut ndays = 0;
if year < 0 {
let excess = 1 + (-year) / 400;
year += excess * 400;
ndays -= excess * 146_097;
}
let div_100 = year / 100;
ndays += ((year * 1461) >> 2) - div_100 + (div_100 >> 2);
ndays + self.ordinal() as i32
}

/// The minimum possible `NaiveDate` (January 1, 262144 BCE).
pub const MIN: NaiveDate = NaiveDate { ymdf: (MIN_YEAR << 13) | (1 << 4) | 0o12 /*D*/ };
/// The maximum possible `NaiveDate` (December 31, 262142 CE).
Expand Down
64 changes: 33 additions & 31 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@
#[deprecated(since = "0.4.23", note = "use `from_timestamp_opt()` instead")]
#[inline]
#[must_use]
pub fn from_timestamp(secs: i64, nsecs: u32) -> NaiveDateTime {
pub const fn from_timestamp(secs: i64, nsecs: u32) -> NaiveDateTime {

Check warning on line 129 in src/naive/datetime/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/datetime/mod.rs#L129

Added line #L129 was not covered by tests
let datetime = NaiveDateTime::from_timestamp_opt(secs, nsecs);
datetime.expect("invalid or out-of-range datetime")
expect!(datetime, "invalid or out-of-range datetime")

Check warning on line 131 in src/naive/datetime/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/naive/datetime/mod.rs#L131

Added line #L131 was not covered by tests
}

/// Creates a new [NaiveDateTime] from milliseconds since the UNIX epoch.
Expand Down Expand Up @@ -157,7 +157,7 @@
/// ```
#[inline]
#[must_use]
pub fn from_timestamp_millis(millis: i64) -> Option<NaiveDateTime> {
pub const fn from_timestamp_millis(millis: i64) -> Option<NaiveDateTime> {
let secs = millis.div_euclid(1000);
let nsecs = millis.rem_euclid(1000) as u32 * 1_000_000;
NaiveDateTime::from_timestamp_opt(secs, nsecs)
Expand Down Expand Up @@ -189,7 +189,7 @@
/// ```
#[inline]
#[must_use]
pub fn from_timestamp_micros(micros: i64) -> Option<NaiveDateTime> {
pub const fn from_timestamp_micros(micros: i64) -> Option<NaiveDateTime> {
let secs = micros.div_euclid(1_000_000);
let nsecs = micros.rem_euclid(1_000_000) as u32 * 1000;
NaiveDateTime::from_timestamp_opt(secs, nsecs)
Expand Down Expand Up @@ -227,13 +227,14 @@
/// ```
#[inline]
#[must_use]
pub fn from_timestamp_opt(secs: i64, nsecs: u32) -> Option<NaiveDateTime> {
pub const fn from_timestamp_opt(secs: i64, nsecs: u32) -> Option<NaiveDateTime> {
let days = secs.div_euclid(86_400);
let secs = secs.rem_euclid(86_400);
let date = i32::try_from(days)
.ok()
.and_then(|days| days.checked_add(719_163))
.and_then(NaiveDate::from_num_days_from_ce_opt);
if days < i32::MIN as i64 || days > i32::MAX as i64 {
return None;
}
let date =
NaiveDate::from_num_days_from_ce_opt(try_opt!((days as i32).checked_add(719_163)));
let time = NaiveTime::from_num_seconds_from_midnight_opt(secs as u32, nsecs);
match (date, time) {
(Some(date), Some(time)) => Some(NaiveDateTime { date, time }),
Expand Down Expand Up @@ -397,10 +398,10 @@
/// ```
#[inline]
#[must_use]
pub fn timestamp(&self) -> i64 {
pub const fn timestamp(&self) -> i64 {
const UNIX_EPOCH_DAY: i64 = 719_163;
let gregorian_day = i64::from(self.date.num_days_from_ce());
let seconds_from_midnight = i64::from(self.time.num_seconds_from_midnight());
let gregorian_day = self.date.num_days_from_ce() as i64;
let seconds_from_midnight = self.time.num_seconds_from_midnight() as i64;
(gregorian_day - UNIX_EPOCH_DAY) * 86_400 + seconds_from_midnight
}

Expand All @@ -425,9 +426,9 @@
/// ```
#[inline]
#[must_use]
pub fn timestamp_millis(&self) -> i64 {
pub const fn timestamp_millis(&self) -> i64 {
let as_ms = self.timestamp() * 1000;
as_ms + i64::from(self.timestamp_subsec_millis())
as_ms + self.timestamp_subsec_millis() as i64
}

/// Returns the number of non-leap *microseconds* since midnight on January 1, 1970.
Expand All @@ -448,9 +449,9 @@
/// ```
#[inline]
#[must_use]
pub fn timestamp_micros(&self) -> i64 {
pub const fn timestamp_micros(&self) -> i64 {
let as_us = self.timestamp() * 1_000_000;
as_us + i64::from(self.timestamp_subsec_micros())
as_us + self.timestamp_subsec_micros() as i64
}

/// Returns the number of non-leap *nanoseconds* since midnight on January 1, 1970.
Expand Down Expand Up @@ -547,7 +548,7 @@
/// ```
#[inline]
#[must_use]
pub fn timestamp_subsec_millis(&self) -> u32 {
pub const fn timestamp_subsec_millis(&self) -> u32 {
self.timestamp_subsec_nanos() / 1_000_000
}

Expand All @@ -569,7 +570,7 @@
/// ```
#[inline]
#[must_use]
pub fn timestamp_subsec_micros(&self) -> u32 {
pub const fn timestamp_subsec_micros(&self) -> u32 {
self.timestamp_subsec_nanos() / 1_000
}

Expand All @@ -591,7 +592,7 @@
/// ```
#[inline]
#[must_use]
pub fn timestamp_subsec_nanos(&self) -> u32 {
pub const fn timestamp_subsec_nanos(&self) -> u32 {
self.time.nanosecond()
}

Expand Down Expand Up @@ -671,7 +672,7 @@
return None;
}

let date = self.date.checked_add_signed(OldDuration::seconds(rhs))?;
let date = try_opt!(self.date.checked_add_signed(OldDuration::seconds(rhs)));
Some(NaiveDateTime { date, time })
}

Expand Down Expand Up @@ -701,8 +702,8 @@
/// );
/// ```
#[must_use]
pub fn checked_add_months(self, rhs: Months) -> Option<NaiveDateTime> {
Some(Self { date: self.date.checked_add_months(rhs)?, time: self.time })
pub const fn checked_add_months(self, rhs: Months) -> Option<NaiveDateTime> {
Some(Self { date: try_opt!(self.date.checked_add_months(rhs)), time: self.time })
}

/// Adds given `FixedOffset` to the current datetime.
Expand Down Expand Up @@ -854,24 +855,24 @@
/// );
/// ```
#[must_use]
pub fn checked_sub_months(self, rhs: Months) -> Option<NaiveDateTime> {
Some(Self { date: self.date.checked_sub_months(rhs)?, time: self.time })
pub const fn checked_sub_months(self, rhs: Months) -> Option<NaiveDateTime> {
Some(Self { date: try_opt!(self.date.checked_sub_months(rhs)), time: self.time })
}

/// Add a duration in [`Days`] to the date part of the `NaiveDateTime`
///
/// Returns `None` if the resulting date would be out of range.
#[must_use]
pub fn checked_add_days(self, days: Days) -> Option<Self> {
Some(Self { date: self.date.checked_add_days(days)?, ..self })
pub const fn checked_add_days(self, days: Days) -> Option<Self> {
Some(Self { date: try_opt!(self.date.checked_add_days(days)), ..self })
}

/// Subtract a duration in [`Days`] from the date part of the `NaiveDateTime`
///
/// Returns `None` if the resulting date would be out of range.
#[must_use]
pub fn checked_sub_days(self, days: Days) -> Option<Self> {
Some(Self { date: self.date.checked_sub_days(days)?, ..self })
pub const fn checked_sub_days(self, days: Days) -> Option<Self> {
Some(Self { date: try_opt!(self.date.checked_sub_days(days)), ..self })
}

/// Subtracts another `NaiveDateTime` from the current date and time.
Expand Down Expand Up @@ -1029,8 +1030,9 @@
/// 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 Expand Up @@ -1430,7 +1432,7 @@
/// The range from 1,000,000,000 to 1,999,999,999 represents
/// the [leap second](./struct.NaiveTime.html#leap-second-handling).
///
/// See also the [`NaiveTime::nanosecond`] method.
/// See also the [`NaiveTime#method.nanosecond`] method.
///
/// # Example
///
Expand Down
15 changes: 15 additions & 0 deletions src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,21 @@ impl NaiveTime {
(hour, min, sec)
}

/// Returns the number of non-leap seconds past the last midnight.
// This duplicates `Timelike::num_seconds_from_midnight()`, because trait methods can't be const
// yet.
#[inline]
pub(crate) const fn num_seconds_from_midnight(&self) -> u32 {
self.secs
}

/// Returns the number of nanoseconds since the whole non-leap second.
// This duplicates `Timelike::nanosecond()`, because trait methods can't be const yet.
#[inline]
pub(crate) const fn nanosecond(&self) -> u32 {
self.frac
}

/// The earliest possible `NaiveTime`
pub const MIN: Self = Self { secs: 0, frac: 0 };
pub(super) const MAX: Self = Self { secs: 23 * 3600 + 59 * 60 + 59, frac: 999_999_999 };
Expand Down
Loading