use jiff::RoundMode;
use jiff::ToSpan;
use jiff::Unit;
use jiff::Zoned;
use jiff::ZonedRound;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Rotation {
Minutely,
Hourly,
Daily,
Never,
}
impl Rotation {
pub fn next_date_timestamp(&self, current_date: &Zoned) -> Option<usize> {
let round = ZonedRound::new().mode(RoundMode::Trunc);
let next_date = match *self {
Rotation::Never => return None,
Rotation::Minutely => (current_date + 1.minute()).round(round.smallest(Unit::Minute)),
Rotation::Hourly => (current_date + 1.hour()).round(round.smallest(Unit::Hour)),
Rotation::Daily => (current_date + 1.day()).round(round.smallest(Unit::Day)),
};
let next_date =
next_date.expect("invalid time; this is a bug in logforth rolling file appender");
Some(next_date.timestamp().as_millisecond() as usize)
}
pub fn date_format(&self) -> &'static str {
match *self {
Rotation::Minutely => "%F-%H-%M",
Rotation::Hourly => "%F-%H",
Rotation::Daily => "%F",
Rotation::Never => "%F",
}
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use jiff::Timestamp;
use jiff::Zoned;
use super::Rotation;
#[test]
fn test_next_date_timestamp() {
let current_date = Zoned::from_str("2024-08-10T17:12:52+08[+08]").unwrap();
assert_eq!(Rotation::Never.next_date_timestamp(¤t_date), None);
let expected_date = "2024-08-10T17:13:00+08".parse::<Timestamp>().unwrap();
assert_eq!(
Rotation::Minutely.next_date_timestamp(¤t_date),
Some(expected_date.as_millisecond() as usize)
);
let expected_date = "2024-08-10T18:00:00+08".parse::<Timestamp>().unwrap();
assert_eq!(
Rotation::Hourly.next_date_timestamp(¤t_date),
Some(expected_date.as_millisecond() as usize)
);
let expected_date = "2024-08-11T00:00:00+08".parse::<Timestamp>().unwrap();
assert_eq!(
Rotation::Daily.next_date_timestamp(¤t_date),
Some(expected_date.as_millisecond() as usize)
);
}
}