Salamek / cron-descriptor

A Python library that converts cron expressions into human readable strings.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error parsing day-of-month combined with day-of-week

dswhite42 opened this issue · comments

Hello. Thank you very much for this library. It seems to be working well, except for a couple of corner-case issues. Here's some code:

import cron_descriptor
cron = '30 4 1,15 * FRI'
print(f'Description for [{cron}] is: {cron_descriptor.get_description(cron)}')

Output:

Description for [30 4 1,15 * FRI] is: At 04:30 AM, on day 1 and 15 of the month, only on Friday

This should actually be something like:

Description for [30 4 1,15 * FRI] is: At 04:30 AM, on day 1 and 15 of the month, plus every Friday

This is in keeping with the crontab(5) man page which indicates the day-of-month and day-of-week criteria should be combined with OR rather than AND.

Note: The day of a command's execution can be specified by two fields — day of month, and day of week. If both fields are restricted (i.e., aren't *), the command will be run when either field matches the current time. For example,
``30 4 1,15 * 5'' would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.

However, there is an exception to this due to a bug in Vixie cron/ISC cron/cronie. Because cron fails to parse asterisk fields that contain more than just a single asterisk (for example, */2), it will join the criteria together with AND rather than OR for those entries, in violation of its own man page. So for this code:

cron = '30 4 */2 * FRI'
print(f'Description for [{cron}] is: {cron_descriptor.get_description(cron)}')

cron-descriptor actually evaluates the code the same way the buggy cron does:

Description for [30 4 */2 * FRI] is: At 04:30 AM, every 2 days, only on Friday

(more details on the asterisk bug in cron is here: https://crontab.guru/cron-bug.html The consensus seems to be that this bug should not be fixed due to its having been around for decades now.)

So, could the incorrect evaluation of cron entries like "30 4 1,5 * FRI" be fixed, while still keeping consistency with the buggy versions of cron for entries like "30 4 */2 * FRI"?

Thanks for looking into it, and for your work on this tool.

Hi, first thanks for this library - it's been very useful.

I've also run into an issue with this.
An example cron string I have: "0 18 27 10 4"

This library then returns the value:

>>> from cron_descriptor import get_description as get_cron_description
>>> get_cron_description("0 18 27 10 4")
'At 06:00 PM, on day 27 of the month, only on Thursday, only in October'

But looking at other resources like:
https://crontab.cronhub.io/
https://cronexpressiontogo.com/

I see "human-readable" values that say "...and on Thursday..." as opposed to "...only on..."

Examples:

At 06:00 PM, on day 27 of the month, and on Thursday, only in October
At 18:00, on day 27 of the month, and on Thursday, only in October

Is it possible to update this library to make a change to that wording?
Thanks again.