Skip to content

Aliases (@hourly, @weekly, etc.) don't work with with_seconds_required option #9

@azasypkin

Description

@azasypkin

Summary

It looks like it's not possible to use the same Cron config to accept both aliases like @hourly or @weekly and standard cron expressions while requiring a seconds specifier.

let get_cron = |pattern: &str| {
    Cron::new(pattern)
        .with_seconds_required()
        .with_dom_and_dow()
        .parse()
};

get_cron("0 0 * * * *")?; -> works as expected

get_cron("@hourly")?; -> fails with `Pattern must consist of six fields, seconds can not be omitted.`

I assume it's because of this mapping:

croner-rust/src/pattern.rs

Lines 175 to 182 in 830fc53

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

To support both, consumers of croner essentially have to re-implement the handle_nicknames logic to detect aliases and omit with_seconds_required, or even manually convert to standard syntax if the expression is just passed along to be parsed elsewhere (e.g., when croner is a transitive dependency). I ran into this issue with https://github.com/mvniekerk/tokio-cron-scheduler, which recently switched to croner from cron.

Thanks for the great library!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions