demit 1.0.0

A flexible data generator for various domains
Documentation
use rand::Rng;
pub struct DateTime;

impl DateTime {
    pub fn gen_rfc3339(years_ago: i64) -> String {
        let base = chrono::Utc::now();

        // Ensure we have at least 1 day to work with
        let days = std::cmp::max(1, years_ago * 365);
        // 24 hours * 60 minutes * 60 seconds = 86400 seconds in a day
        let seconds_in_day = 86400;

        let mut rng = rand::rng();

        // create a random date and time
        let mut date =
            base - chrono::Duration::days(rng.random_range(1..=days));
        date -= chrono::Duration::seconds(rng.random_range(1..=seconds_in_day));
        date.to_rfc3339()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_gen_rfc3339() {
        let date = DateTime::gen_rfc3339(1);

        // assert that the date is in the past
        assert!(
            chrono::Utc::now()
                > chrono::DateTime::parse_from_rfc3339(&date).unwrap()
        );
    }
}