Files
Olivier 'reivilibre ab0b1e84ee
Some checks failed
continuous-integration/drone the build failed
STASH work on Raking
2022-03-19 21:04:12 +00:00

24 lines
824 B
Rust

use anyhow::Context;
use chrono::{Date, Duration, TimeZone, Utc};
use lazy_static::lazy_static;
lazy_static! {
/// The QuickPeep Epoch is 2022-01-01, as this gives us 52 years of extra headroom compared to the
/// Unix one. QuickPeep didn't exist before 2022 so we needn't worry about negative dates!
pub static ref QUICKPEEP_EPOCH: Date<Utc> = Utc.ymd(2022, 1, 1);
}
pub fn date_from_quickpeep_days(days: u16) -> Date<Utc> {
let dt = QUICKPEEP_EPOCH.and_hms(0, 0, 0);
(dt + Duration::days(days as i64)).date()
}
pub fn date_to_quickpeep_days(date: &Date<Utc>) -> anyhow::Result<u16> {
let dt = date.and_hms(0, 0, 0);
let duration = dt - QUICKPEEP_EPOCH.and_hms(0, 0, 0);
duration
.num_days()
.try_into()
.context("Failed to convert date to QuickPeep datestamp")
}