use chrono::{DateTime, Utc};
use time::Duration;
pub fn days_ago(n: i64) -> DateTime<Utc> {
Utc::now() - Duration::days(n)
}
pub fn after(base: impl Into<DateTime<Utc>>, after: Duration) -> DateTime<Utc> {
base.into() + after
}
#[macro_export]
macro_rules! at {
(now) => {
chrono::Utc::now()
};
($m:expr, minutes ago) => {
chrono::Utc::now() - time::Duration::minutes($m)
};
($h:expr, hours ago) => {
chrono::Utc::now() - time::Duration::hours($h)
};
($d:expr, days ago) => {
chrono::Utc::now() - time::Duration::days($d)
};
($w:expr, weeks ago) => {
chrono::Utc::now() - time::Duration::weeks($w)
};
($base:expr => $m:expr, minutes) => {
$crate::datetime::after($base, time::Duration::minutes($m))
};
($base:expr => $h:expr, hours) => {
$crate::datetime::after($base, time::Duration::hours($h))
};
($base:expr => $d:expr, days) => {
$crate::datetime::after($base, time::Duration::days($d))
};
($base:expr => $w:expr, weeks) => {
$crate::datetime::after($base, time::Duration::weeks($w))
};
(now => $m:expr, minutes) => {
$crate::datetime::after(chrono::Utc::now(), time::Duration::minutes($m))
};
(now => $h:expr, hours) => {
$crate::datetime::after(chrono::Utc::now(), time::Duration::hours($h))
};
(now => $d:expr, days) => {
$crate::datetime::after(chrono::Utc::now(), time::Duration::days($d))
};
(now => $w:expr, weeks) => {
$crate::datetime::after(chrono::Utc::now(), time::Duration::weeks($w))
};
}
#[macro_export]
macro_rules! day {
($y:literal, $m:literal, $d:literal) => {
chrono::DateTime::parse_from_rfc3339(
format!("{:04}-{:02}-{:02}T00:00:00.000000-00:00", $y, $m, $d).as_str(),
)
};
($y:literal, $m:literal, $d:literal, start) => {
chrono::DateTime::parse_from_rfc3339(
format!("{:04}-{:02}-{:02}T00:00:00.000000-00:00", $y, $m, $d).as_str(),
)
};
($y:literal, $m:literal, $d:literal, end) => {
chrono::DateTime::parse_from_rfc3339(
format!("{:04}-{:02}-{:02}T23:59:59.000000-00:00", $y, $m, $d).as_str(),
)
};
}
pub fn vtdatetime(datetime: impl Into<DateTime<Utc>>) -> String {
datetime.into().format("%FT%T").to_string()
}