From f4240aa62b47850020aa8c3e164d6d3544626f53 Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Wed, 13 Apr 2022 18:29:18 +0100 Subject: Initial implementation of calendar API (#298) This can be used in the future for sync so that we can be more intelligent with what we're doing, and only sync up what's needed I'd like to eventually replace this with something more like a merkle tree, hence the hash field I've exposed, but that can come later Although this does include a much larger number of count queries, it should also be significantly more cache-able. I'll follow up with that later, and also follow up with using this for sync :) --- atuin-common/src/calendar.rs | 15 +++++++++++++++ atuin-common/src/utils.rs | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 atuin-common/src/calendar.rs (limited to 'atuin-common/src') diff --git a/atuin-common/src/calendar.rs b/atuin-common/src/calendar.rs new file mode 100644 index 00000000..d576f1a7 --- /dev/null +++ b/atuin-common/src/calendar.rs @@ -0,0 +1,15 @@ +// Calendar data + +pub enum TimePeriod { + YEAR, + MONTH, + DAY, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct TimePeriodInfo { + pub count: u64, + + // TODO: Use this for merkle tree magic + pub hash: String, +} diff --git a/atuin-common/src/utils.rs b/atuin-common/src/utils.rs index 7fe0c300..35647bd4 100644 --- a/atuin-common/src/utils.rs +++ b/atuin-common/src/utils.rs @@ -1,5 +1,6 @@ use std::path::PathBuf; +use chrono::NaiveDate; use crypto::digest::Digest; use crypto::sha2::Sha256; use sodiumoxide::crypto::pwhash::argon2id13; @@ -51,6 +52,22 @@ pub fn data_dir() -> PathBuf { data_dir.join("atuin") } +pub fn get_days_from_month(year: i32, month: u32) -> i64 { + NaiveDate::from_ymd( + match month { + 12 => year + 1, + _ => year, + }, + match month { + 12 => 1, + _ => month + 1, + }, + 1, + ) + .signed_duration_since(NaiveDate::from_ymd(year, month, 1)) + .num_days() +} + #[cfg(test)] mod tests { use super::*; -- cgit v1.3.1