diff options
Diffstat (limited to 'atuin-common')
| -rw-r--r-- | atuin-common/Cargo.toml | 5 | ||||
| -rw-r--r-- | atuin-common/src/api.rs | 11 | ||||
| -rw-r--r-- | atuin-common/src/record.rs | 2 | ||||
| -rw-r--r-- | atuin-common/src/utils.rs | 44 |
4 files changed, 30 insertions, 32 deletions
diff --git a/atuin-common/Cargo.toml b/atuin-common/Cargo.toml index a610584d..88c3022e 100644 --- a/atuin-common/Cargo.toml +++ b/atuin-common/Cargo.toml @@ -1,8 +1,9 @@ [package] name = "atuin-common" -edition = "2018" +edition = "2021" description = "common library for atuin" +rust-version = { workspace = true } version = { workspace = true } authors = { workspace = true } license = { workspace = true } @@ -12,7 +13,7 @@ repository = { workspace = true } # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -chrono = { workspace = true } +time = { workspace = true } serde = { workspace = true } uuid = { workspace = true } rand = { workspace = true } diff --git a/atuin-common/src/api.rs b/atuin-common/src/api.rs index 5622bd82..ddcc0b09 100644 --- a/atuin-common/src/api.rs +++ b/atuin-common/src/api.rs @@ -1,6 +1,6 @@ -use chrono::Utc; use serde::{Deserialize, Serialize}; use std::borrow::Cow; +use time::OffsetDateTime; #[derive(Debug, Serialize, Deserialize)] pub struct UserResponse { @@ -36,7 +36,8 @@ pub struct LoginResponse { #[derive(Debug, Serialize, Deserialize)] pub struct AddHistoryRequest { pub id: String, - pub timestamp: chrono::DateTime<Utc>, + #[serde(with = "time::serde::rfc3339")] + pub timestamp: OffsetDateTime, pub data: String, pub hostname: String, } @@ -48,8 +49,10 @@ pub struct CountResponse { #[derive(Debug, Serialize, Deserialize)] pub struct SyncHistoryRequest { - pub sync_ts: chrono::DateTime<chrono::FixedOffset>, - pub history_ts: chrono::DateTime<chrono::FixedOffset>, + #[serde(with = "time::serde::rfc3339")] + pub sync_ts: OffsetDateTime, + #[serde(with = "time::serde::rfc3339")] + pub history_ts: OffsetDateTime, pub host: String, } diff --git a/atuin-common/src/record.rs b/atuin-common/src/record.rs index b00c03c4..cba0917a 100644 --- a/atuin-common/src/record.rs +++ b/atuin-common/src/record.rs @@ -42,7 +42,7 @@ pub struct Record<Data> { pub parent: Option<RecordId>, /// The creation time in nanoseconds since unix epoch - #[builder(default = chrono::Utc::now().timestamp_nanos() as u64)] + #[builder(default = time::OffsetDateTime::now_utc().unix_timestamp_nanos() as u64)] pub timestamp: u64, /// The version the data in the entry conforms to diff --git a/atuin-common/src/utils.rs b/atuin-common/src/utils.rs index ed98e275..7cf4e9de 100644 --- a/atuin-common/src/utils.rs +++ b/atuin-common/src/utils.rs @@ -1,7 +1,6 @@ use std::env; use std::path::PathBuf; -use chrono::{Months, NaiveDate}; use rand::RngCore; use uuid::Uuid; @@ -37,7 +36,10 @@ const fn encode_unix_timestamp_millis(millis: u64, random_bytes: &[u8; 10]) -> U pub fn uuid_v7() -> Uuid { let bytes = random_bytes(); - let now: u64 = chrono::Utc::now().timestamp_millis() as u64; + let now: u64 = u64::try_from( + time::OffsetDateTime::now_utc().unix_timestamp_nanos() / 1_000_000, + ) + .expect("Either you're in the past (1970) - or your in the far future (2554). Good for you"); encode_unix_timestamp_millis(now, &bytes) } @@ -111,18 +113,10 @@ pub fn get_current_dir() -> String { } } -pub fn get_days_from_month(year: i32, month: u32) -> i64 { - let Some(start) = NaiveDate::from_ymd_opt(year, month, 1) else { - return 30; - }; - let Some(end) = start.checked_add_months(Months::new(1)) else { - return 30; - }; - end.signed_duration_since(start).num_days() -} - #[cfg(test)] mod tests { + use time::Month; + use super::*; use std::env; @@ -170,21 +164,21 @@ mod tests { #[test] fn days_from_month() { - assert_eq!(get_days_from_month(2023, 1), 31); - assert_eq!(get_days_from_month(2023, 2), 28); - assert_eq!(get_days_from_month(2023, 3), 31); - assert_eq!(get_days_from_month(2023, 4), 30); - assert_eq!(get_days_from_month(2023, 5), 31); - assert_eq!(get_days_from_month(2023, 6), 30); - assert_eq!(get_days_from_month(2023, 7), 31); - assert_eq!(get_days_from_month(2023, 8), 31); - assert_eq!(get_days_from_month(2023, 9), 30); - assert_eq!(get_days_from_month(2023, 10), 31); - assert_eq!(get_days_from_month(2023, 11), 30); - assert_eq!(get_days_from_month(2023, 12), 31); + assert_eq!(time::util::days_in_year_month(2023, Month::January), 31); + assert_eq!(time::util::days_in_year_month(2023, Month::February), 28); + assert_eq!(time::util::days_in_year_month(2023, Month::March), 31); + assert_eq!(time::util::days_in_year_month(2023, Month::April), 30); + assert_eq!(time::util::days_in_year_month(2023, Month::May), 31); + assert_eq!(time::util::days_in_year_month(2023, Month::June), 30); + assert_eq!(time::util::days_in_year_month(2023, Month::July), 31); + assert_eq!(time::util::days_in_year_month(2023, Month::August), 31); + assert_eq!(time::util::days_in_year_month(2023, Month::September), 30); + assert_eq!(time::util::days_in_year_month(2023, Month::October), 31); + assert_eq!(time::util::days_in_year_month(2023, Month::November), 30); + assert_eq!(time::util::days_in_year_month(2023, Month::December), 31); // leap years - assert_eq!(get_days_from_month(2024, 2), 29); + assert_eq!(time::util::days_in_year_month(2024, Month::February), 29); } #[test] |
