Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autodetect parse with seconds #8

Open
SylwesterZarebski opened this issue Mar 30, 2018 · 8 comments
Open

Autodetect parse with seconds #8

SylwesterZarebski opened this issue Mar 30, 2018 · 8 comments
Labels

Comments

@SylwesterZarebski
Copy link

Hi,
I'm using Cronos inside application which read user configurations which could have standard minutes form or extended with seconds. Problem is, i do not know if user enter one or another and Parse() method without second parameter throws Cronos.CronFormatException: Unexpected character '*'..
How should i detect if it is standard form or extended when Cronos can't detect it by itself?

@odinserj
Copy link
Member

The * * * * * * expression is ambiguous – some parsers treat the first part as a second value, other parsers treat the first part as a minute, and last part as an year value – that's why there's no auto-detection mechanism in Cronos for this case.

If your values only include seconds as a six-part expressions, then you can use the String.Split method to count the number of parts in a cron expression, and decide to use the corresponding format based on the result.

@SylwesterZarebski
Copy link
Author

SylwesterZarebski commented Mar 30, 2018

Thanks, i will use counting method - i wasn't sure that simple field counting will be enough.

Also it could be new feature to Cronos to also autodetect the same way, because Cronos do not use any other six-part syntax than seconds as documented (or support but it is not documented?).

@odinserj
Copy link
Member

Maybe years will be added, may be not (I don't see any useful use case for them, but let's wait and see), but in general case (with years), reliable automatic parsing can't be performed. The only we can do to avoid any ambiguities is to add a static method that counts how much fields there is in a given cron expression to retain the parsing logic encapsulated, and not to shoot one's foot later.

@SylwesterZarebski
Copy link
Author

Thanks and Happy Easter :-).

@odinserj
Copy link
Member

Not a problem, I'm just trying to avoid ambiguities as much as possible, because completely tired of them😳.

@lucashutyler
Copy link

lucashutyler commented Oct 7, 2020

I think this is the best place to leave this comment, but one thing that really got me confused recently was the following issue:

var myCron = "30 8 * * 1"; // every monday at 8:30
var cron = CronExpression.Parse(myCron).ToString(); // successfully parse, return string format (with seconds!!!)
cron = CronExpression.Parse(cron).ToString(); // fails to parse because it includes seconds

Question: Why does the Parse method expect no seconds by default (I know CronFormat.IncludeSeconds exists), but the ToString() returns seconds format by default? I would expect this to be a repeatable pattern.

@jbennink
Copy link

jbennink commented Dec 7, 2022

Wow reading this issue last but this also relates to #15

@jbennink
Copy link

jbennink commented Dec 7, 2022

@odinserj I wondered why cron does not include years for detecting special holidays or special open/closed periods that only exist in that specific year. This relates to cron expression used in Asterisk dialplans where you want to be closed in 2022 on the 10 of October. But the ambiguity with seconds is also something

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants