diff options
Diffstat (limited to 'atuin-common/src/utils.rs')
| -rw-r--r-- | atuin-common/src/utils.rs | 44 |
1 files changed, 19 insertions, 25 deletions
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] |
