Skip to content

Commit

Permalink
Fix alias with seconds issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Sep 26, 2024
1 parent 830fc53 commit 1b895fb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
48 changes: 34 additions & 14 deletions src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl CronPattern {

// Handle @nicknames
if self.pattern.contains('@') {
self.pattern = Self::handle_nicknames(&self.pattern).trim().to_string();
self.pattern = Self::handle_nicknames(&self.pattern, self.with_seconds_required).trim().to_string();
}

// Handle day-of-week and month aliases (MON... and JAN...)
Expand Down Expand Up @@ -165,20 +165,32 @@ impl CronPattern {
Ok(())
}


// Converts named cron pattern shortcuts like '@daily' into their equivalent standard cron pattern.
fn handle_nicknames(pattern: &str) -> &str {
fn handle_nicknames(pattern: &str, with_seconds_required: bool) -> &str {
let pattern = pattern.trim();

// 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 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,
}
}
}

Expand Down Expand Up @@ -631,11 +643,19 @@ mod tests {

#[test]
fn test_cron_pattern_handle_nicknames() {
assert_eq!(CronPattern::handle_nicknames("@yearly"), "0 0 1 1 *");
assert_eq!(CronPattern::handle_nicknames("@monthly"), "0 0 1 * *");
assert_eq!(CronPattern::handle_nicknames("@weekly"), "0 0 * * 0");
assert_eq!(CronPattern::handle_nicknames("@daily"), "0 0 * * *");
assert_eq!(CronPattern::handle_nicknames("@hourly"), "0 * * * *");
assert_eq!(CronPattern::handle_nicknames("@yearly", false), "0 0 1 1 *");
assert_eq!(CronPattern::handle_nicknames("@monthly", false), "0 0 1 * *");
assert_eq!(CronPattern::handle_nicknames("@weekly", false), "0 0 * * 0");
assert_eq!(CronPattern::handle_nicknames("@daily", false), "0 0 * * *");
assert_eq!(CronPattern::handle_nicknames("@hourly", false), "0 * * * *");
}
#[test]
fn test_cron_pattern_handle_nicknames_with_seconds_required() {
assert_eq!(CronPattern::handle_nicknames("@yearly", true), "0 0 0 1 1 *");
assert_eq!(CronPattern::handle_nicknames("@monthly", true), "0 0 0 1 * *");
assert_eq!(CronPattern::handle_nicknames("@weekly", true), "0 0 0 * * 0");
assert_eq!(CronPattern::handle_nicknames("@daily", true), "0 0 0 * * *");
assert_eq!(CronPattern::handle_nicknames("@hourly", true), "0 0 * * * *");
}

#[test]
Expand Down

0 comments on commit 1b895fb

Please sign in to comment.