summaryrefslogtreecommitdiff
path: root/schist_core/schist_models/src/datetime_utc.rs
diff options
context:
space:
mode:
authorJoe Carstairs <me@joeac.net>2025-08-16 07:46:21 +0100
committerJoe Carstairs <me@joeac.net>2025-08-16 18:22:47 +0100
commitd082a30de6a0cbd1fbe2ab5a4316db0891004129 (patch)
treec097414625e1948116383406d894842fccc98227 /schist_core/schist_models/src/datetime_utc.rs
parent9c0bc43bf0b83828d9bdf8005fcccbc33dcf4171 (diff)
refactor models and traits with pub use
Diffstat (limited to 'schist_core/schist_models/src/datetime_utc.rs')
-rw-r--r--schist_core/schist_models/src/datetime_utc.rs99
1 files changed, 70 insertions, 29 deletions
diff --git a/schist_core/schist_models/src/datetime_utc.rs b/schist_core/schist_models/src/datetime_utc.rs
index 9b63145..4f3b165 100644
--- a/schist_core/schist_models/src/datetime_utc.rs
+++ b/schist_core/schist_models/src/datetime_utc.rs
@@ -12,19 +12,10 @@ use diesel::{
};
use serde::{Deserialize, Serialize};
-use schist_traits::{dateable::Dateable, nowlike::Nowlike, timeable::Timeable};
+use schist_traits::{Dateable, Nowlike, Timeable};
#[derive(
- PartialEq,
- Eq,
- PartialOrd,
- Ord,
- Serialize,
- Deserialize,
- Clone,
- Debug,
- AsExpression,
- FromSqlRow,
+ PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Clone, Debug, AsExpression, FromSqlRow,
)]
#[diesel(sql_type=Text)]
pub struct DatetimeUtc {
@@ -32,13 +23,30 @@ pub struct DatetimeUtc {
}
impl DatetimeUtc {
- pub fn from_ymd_and_hms(year: i32, month: u32, day: u32, hour: u32, min: u32, sec: u32) -> Result<Self> {
- Ok(Self { chrono_datetime_utc: NaiveDate
- ::from_ymd_opt(year, month, day)
- .with_context(|| format!("failed to construct datetime from ymd {:04}-{:02}-{:02}", year, month, day))?
- .and_hms_opt(hour, min, sec)
- .with_context(|| format!("failed to construct datetime from hms {:02}:{:02}:{:02}", hour, min, sec))?
- .and_utc()
+ pub fn from_ymd_and_hms(
+ year: i32,
+ month: u32,
+ day: u32,
+ hour: u32,
+ min: u32,
+ sec: u32,
+ ) -> Result<Self> {
+ Ok(Self {
+ chrono_datetime_utc: NaiveDate::from_ymd_opt(year, month, day)
+ .with_context(|| {
+ format!(
+ "failed to construct datetime from ymd {:04}-{:02}-{:02}",
+ year, month, day
+ )
+ })?
+ .and_hms_opt(hour, min, sec)
+ .with_context(|| {
+ format!(
+ "failed to construct datetime from hms {:02}:{:02}:{:02}",
+ hour, min, sec
+ )
+ })?
+ .and_utc(),
})
}
}
@@ -60,7 +68,12 @@ impl Display for DatetimeUtc {
/// Converts to a date string in ISO format, in UTC
/// Example: 10.30am 24 Oct 2024 UTC -> "2024-10-24T10:30:00.000Z"
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
- write!(f, "{}", self.chrono_datetime_utc.to_rfc3339_opts(chrono::SecondsFormat::Millis, true))
+ write!(
+ f,
+ "{}",
+ self.chrono_datetime_utc
+ .to_rfc3339_opts(chrono::SecondsFormat::Millis, true)
+ )
}
}
@@ -74,7 +87,8 @@ impl Nowlike for DatetimeUtc {
now.hour(),
now.minute(),
now.second(),
- ).unwrap()
+ )
+ .unwrap()
}
}
@@ -161,21 +175,45 @@ mod test {
let datetime_utc_8 = DatetimeUtc::from_ymd_and_hms(1970, 11, 23, 0, 0, 61);
assert!(datetime_utc_1.is_err());
- assert_eq!(datetime_utc_1.unwrap_err().to_string(), "failed to construct datetime from ymd 1970-00-23");
+ assert_eq!(
+ datetime_utc_1.unwrap_err().to_string(),
+ "failed to construct datetime from ymd 1970-00-23"
+ );
assert!(datetime_utc_2.is_err());
- assert_eq!(datetime_utc_2.unwrap_err().to_string(), "failed to construct datetime from ymd 1970-13-23");
+ assert_eq!(
+ datetime_utc_2.unwrap_err().to_string(),
+ "failed to construct datetime from ymd 1970-13-23"
+ );
assert!(datetime_utc_3.is_err());
- assert_eq!(datetime_utc_3.unwrap_err().to_string(), "failed to construct datetime from ymd 1970-11-00");
+ assert_eq!(
+ datetime_utc_3.unwrap_err().to_string(),
+ "failed to construct datetime from ymd 1970-11-00"
+ );
assert!(datetime_utc_4.is_err());
- assert_eq!(datetime_utc_4.unwrap_err().to_string(), "failed to construct datetime from ymd 1970-11-31");
+ assert_eq!(
+ datetime_utc_4.unwrap_err().to_string(),
+ "failed to construct datetime from ymd 1970-11-31"
+ );
assert!(datetime_utc_5.is_err());
- assert_eq!(datetime_utc_5.unwrap_err().to_string(), "failed to construct datetime from ymd 1971-02-29");
+ assert_eq!(
+ datetime_utc_5.unwrap_err().to_string(),
+ "failed to construct datetime from ymd 1971-02-29"
+ );
assert!(datetime_utc_6.is_err());
- assert_eq!(datetime_utc_6.unwrap_err().to_string(), "failed to construct datetime from hms 25:00:00");
+ assert_eq!(
+ datetime_utc_6.unwrap_err().to_string(),
+ "failed to construct datetime from hms 25:00:00"
+ );
assert!(datetime_utc_7.is_err());
- assert_eq!(datetime_utc_7.unwrap_err().to_string(), "failed to construct datetime from hms 00:61:00");
+ assert_eq!(
+ datetime_utc_7.unwrap_err().to_string(),
+ "failed to construct datetime from hms 00:61:00"
+ );
assert!(datetime_utc_8.is_err());
- assert_eq!(datetime_utc_8.unwrap_err().to_string(), "failed to construct datetime from hms 00:00:61");
+ assert_eq!(
+ datetime_utc_8.unwrap_err().to_string(),
+ "failed to construct datetime from hms 00:00:61"
+ );
}
#[test]
@@ -197,7 +235,10 @@ mod test {
let datetime_utc: Result<DatetimeUtc> = "1970-11-23T12:55:16.000Z".parse();
assert!(datetime_utc.is_ok());
- assert_eq!(datetime_utc.unwrap().to_string(), "1970-11-23T12:55:16.000Z")
+ assert_eq!(
+ datetime_utc.unwrap().to_string(),
+ "1970-11-23T12:55:16.000Z"
+ )
}
#[test]