From 6f06e1ab0eada3b17169ce870a1c7182d59a4da3 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Wed, 31 Jan 2024 20:21:49 +0100 Subject: [PATCH] Make methods on `DateTime` const where possible --- src/datetime/mod.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index 7b0fa842c4..6d79239582 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -200,7 +200,7 @@ impl DateTime { /// ``` #[inline] #[must_use] - pub fn timestamp(&self) -> i64 { + pub const fn timestamp(&self) -> i64 { self.datetime.timestamp() } @@ -219,7 +219,7 @@ impl DateTime { /// ``` #[inline] #[must_use] - pub fn timestamp_millis(&self) -> i64 { + pub const fn timestamp_millis(&self) -> i64 { self.datetime.timestamp_millis() } @@ -238,7 +238,7 @@ impl DateTime { /// ``` #[inline] #[must_use] - pub fn timestamp_micros(&self) -> i64 { + pub const fn timestamp_micros(&self) -> i64 { self.datetime.timestamp_micros() } @@ -254,9 +254,9 @@ impl DateTime { #[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. @@ -294,7 +294,7 @@ impl DateTime { /// ``` #[inline] #[must_use] - pub fn timestamp_nanos_opt(&self) -> Option { + pub const fn timestamp_nanos_opt(&self) -> Option { self.datetime.timestamp_nanos_opt() } @@ -303,7 +303,7 @@ impl DateTime { /// 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() } @@ -312,7 +312,7 @@ impl DateTime { /// 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() } @@ -321,14 +321,14 @@ impl DateTime { /// 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 } @@ -360,7 +360,7 @@ impl DateTime { /// information. #[inline] #[must_use] - pub fn to_utc(&self) -> DateTime { + pub const fn to_utc(&self) -> DateTime { DateTime { datetime: self.datetime, offset: Utc } } @@ -476,7 +476,7 @@ impl DateTime { /// 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 } @@ -658,8 +658,8 @@ impl DateTime { /// ``` #[inline] #[must_use] - pub fn from_timestamp_millis(millis: i64) -> Option { - NaiveDateTime::from_timestamp_millis(millis).as_ref().map(NaiveDateTime::and_utc) + pub const fn from_timestamp_millis(millis: i64) -> Option { + Some(try_opt!(NaiveDateTime::from_timestamp_millis(millis)).and_utc()) } /// The Unix Epoch, 1970-01-01 00:00:00 UTC.