Skip to content

Commit

Permalink
Make methods on DateTime const where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Feb 2, 2024
1 parent fd66791 commit 0e8485e
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// ```
#[inline]
#[must_use]
pub fn timestamp(&self) -> i64 {
pub const fn timestamp(&self) -> i64 {
self.datetime.timestamp()
}

Expand All @@ -219,7 +219,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// ```
#[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 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// ```
#[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 @@ impl<Tz: TimeZone> DateTime<Tz> {
#[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()
}

/// Returns the number of non-leap-nanoseconds since January 1, 1970 UTC.
Expand Down Expand Up @@ -294,7 +294,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// ```
#[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 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// 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 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// 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 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// 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 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// 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 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// 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,7 +658,7 @@ impl DateTime<Utc> {
/// ```
#[inline]
#[must_use]
pub fn from_timestamp_millis(millis: i64) -> Option<Self> {
pub const fn from_timestamp_millis(millis: i64) -> Option<Self> {
NaiveDateTime::from_timestamp_millis(millis).as_ref().map(NaiveDateTime::and_utc)
}

Expand Down

0 comments on commit 0e8485e

Please sign in to comment.