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 DateTime const where possible #1400

Merged
merged 1 commit into from
Feb 2, 2024
Merged
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
30 changes: 15 additions & 15 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
/// ```
#[inline]
#[must_use]
pub fn timestamp(&self) -> i64 {
pub const fn timestamp(&self) -> i64 {
self.datetime.timestamp()
}

Expand All @@ -219,7 +219,7 @@
/// ```
#[inline]
#[must_use]
pub fn timestamp_millis(&self) -> i64 {
pub const fn timestamp_millis(&self) -> i64 {
self.datetime.timestamp_millis()
}

Expand All @@ -238,7 +238,7 @@
/// ```
#[inline]
#[must_use]
pub fn timestamp_micros(&self) -> i64 {
pub const fn timestamp_micros(&self) -> i64 {
self.datetime.timestamp_micros()
}

Expand All @@ -254,9 +254,9 @@
#[deprecated(since = "0.4.31", note = "use `timestamp_nanos_opt()` instead")]
#[inline]
#[must_use]
pub fn timestamp_nanos(&self) -> i64 {
self.timestamp_nanos_opt()
.expect("value can not be represented in a timestamp with nanosecond precision.")
#[allow(deprecated)]
pub const fn timestamp_nanos(&self) -> i64 {
self.datetime.timestamp_nanos()

Check warning on line 259 in src/datetime/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/datetime/mod.rs#L258-L259

Added lines #L258 - L259 were not covered by tests
}

/// Returns the number of non-leap-nanoseconds since January 1, 1970 UTC.
Expand Down Expand Up @@ -294,7 +294,7 @@
/// ```
#[inline]
#[must_use]
pub fn timestamp_nanos_opt(&self) -> Option<i64> {
pub const fn timestamp_nanos_opt(&self) -> Option<i64> {
self.datetime.timestamp_nanos_opt()
}

Expand All @@ -303,7 +303,7 @@
/// In event of a leap second this may exceed 999.
#[inline]
#[must_use]
pub fn timestamp_subsec_millis(&self) -> u32 {
pub const fn timestamp_subsec_millis(&self) -> u32 {
self.datetime.timestamp_subsec_millis()
}

Expand All @@ -312,7 +312,7 @@
/// In event of a leap second this may exceed 999,999.
#[inline]
#[must_use]
pub fn timestamp_subsec_micros(&self) -> u32 {
pub const fn timestamp_subsec_micros(&self) -> u32 {
self.datetime.timestamp_subsec_micros()
}

Expand All @@ -321,14 +321,14 @@
/// In event of a leap second this may exceed 999,999,999.
#[inline]
#[must_use]
pub fn timestamp_subsec_nanos(&self) -> u32 {
pub const fn timestamp_subsec_nanos(&self) -> u32 {
self.datetime.timestamp_subsec_nanos()
}

/// Retrieves an associated offset from UTC.
#[inline]
#[must_use]
pub fn offset(&self) -> &Tz::Offset {
pub const fn offset(&self) -> &Tz::Offset {
&self.offset
}

Expand Down Expand Up @@ -360,7 +360,7 @@
/// information.
#[inline]
#[must_use]
pub fn to_utc(&self) -> DateTime<Utc> {
pub const fn to_utc(&self) -> DateTime<Utc> {
DateTime { datetime: self.datetime, offset: Utc }
}

Expand Down Expand Up @@ -476,7 +476,7 @@
/// Returns a view to the naive UTC datetime.
#[inline]
#[must_use]
pub fn naive_utc(&self) -> NaiveDateTime {
pub const fn naive_utc(&self) -> NaiveDateTime {
self.datetime
}

Expand Down Expand Up @@ -658,8 +658,8 @@
/// ```
#[inline]
#[must_use]
pub fn from_timestamp_millis(millis: i64) -> Option<Self> {
NaiveDateTime::from_timestamp_millis(millis).as_ref().map(NaiveDateTime::and_utc)
pub const fn from_timestamp_millis(millis: i64) -> Option<Self> {
Some(try_opt!(NaiveDateTime::from_timestamp_millis(millis)).and_utc())
}

/// The Unix Epoch, 1970-01-01 00:00:00 UTC.
Expand Down
Loading