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 32ac601
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 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
49 changes: 42 additions & 7 deletions src/pattern.rs
Original file line number Diff line number Diff line change
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 {
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 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,
}
}
}

Expand Down Expand Up @@ -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 *");
Expand Down

0 comments on commit 32ac601

Please sign in to comment.