diff --git a/Cargo.toml b/Cargo.toml index 75a5d68..17e23ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "croner" -version = "2.0.5" +version = "2.0.6" edition = "2021" license = "MIT" description = "Fully-featured, lightweight, and efficient Rust library designed for parsing and evaluating cron patterns" diff --git a/src/pattern.rs b/src/pattern.rs index 9e1fa06..be58b4f 100644 --- a/src/pattern.rs +++ b/src/pattern.rs @@ -165,6 +165,7 @@ impl CronPattern { Ok(()) } + // Converts named cron pattern shortcuts like '@daily' into their equivalent standard cron pattern. fn handle_nicknames(pattern: &str) -> &str { let pattern = pattern.trim(); @@ -172,13 +173,24 @@ impl CronPattern { // Closure that performs a case-insensitive comparison of two strings. let eq_ignore_case = |a: &str, b: &str| a.eq_ignore_ascii_case(b); - match pattern { - p if eq_ignore_case(p, "@yearly") || eq_ignore_case(p, "@annually") => "0 0 1 1 *", - p if eq_ignore_case(p, "@monthly") => "0 0 1 * *", - p if eq_ignore_case(p, "@weekly") => "0 0 * * 0", - p if eq_ignore_case(p, "@daily") => "0 0 * * *", - p if eq_ignore_case(p, "@hourly") => "0 * * * *", - _ => pattern, + if self.with_seconds_required { + match pattern { + p if eq_ignore_case(p, "@yearly") || eq_ignore_case(p, "@annually") => "0 0 0 1 1 *", + p if eq_ignore_case(p, "@monthly") => "0 0 0 1 * *", + p if eq_ignore_case(p, "@weekly") => "0 0 0 * * 0", + p if eq_ignore_case(p, "@daily") => "0 0 0 * * *", + p if eq_ignore_case(p, "@hourly") => "0 0 * * * *", + _ => pattern, + } + } else { + match pattern { + p if eq_ignore_case(p, "@yearly") || eq_ignore_case(p, "@annually") => "0 0 1 1 *", + p if eq_ignore_case(p, "@monthly") => "0 0 1 * *", + p if eq_ignore_case(p, "@weekly") => "0 0 * * 0", + p if eq_ignore_case(p, "@daily") => "0 0 * * *", + p if eq_ignore_case(p, "@hourly") => "0 * * * *", + _ => pattern, + } } } @@ -638,6 +650,29 @@ mod tests { assert_eq!(CronPattern::handle_nicknames("@hourly"), "0 * * * *"); } + #[test] + fn test_cron_pattern_handle_nicknames_with_seconds_required() { + let mut pattern = CronPattern::new("@yearly"); + pattern.with_seconds_required(); + assert_eq!(pattern.parse().unwrap(), "0 0 0 1 1 *"); + + let mut pattern = CronPattern::new("@monthly"); + pattern.with_seconds_required(); + assert_eq!(pattern.parse().unwrap(), "0 0 0 1 * *"); + + let mut pattern = CronPattern::new("@weekly"); + pattern.with_seconds_required(); + assert_eq!(pattern.parse().unwrap(), "0 0 0 * * 0"); + + let mut pattern = CronPattern::new("@daily"); + pattern.with_seconds_required(); + assert_eq!(pattern.parse().unwrap(), "0 0 0 * * *"); + + let mut pattern = CronPattern::new("@hourly"); + pattern.with_seconds_required(); + assert_eq!(pattern.parse().unwrap(), "0 0 * * * *"); + } + #[test] fn test_month_nickname_range() { let mut pattern = CronPattern::new("0 0 * FEB-MAR *");