use rand::Rng;
pub struct DateTime;
impl DateTime {
pub fn gen_rfc3339(years_ago: i64) -> String {
let base = chrono::Utc::now();
let days = std::cmp::max(1, years_ago * 365);
let seconds_in_day = 86400;
let mut rng = rand::rng();
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!(
chrono::Utc::now()
> chrono::DateTime::parse_from_rfc3339(&date).unwrap()
);
}
}