Cronos icon indicating copy to clipboard operation
Cronos copied to clipboard

Autodetect parse with seconds

Open SylwesterZarebski opened this issue 6 years ago • 8 comments

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?

SylwesterZarebski avatar Mar 30 '18 08:03 SylwesterZarebski

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.

odinserj avatar Mar 30 '18 16:03 odinserj

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?).

SylwesterZarebski avatar Mar 30 '18 16:03 SylwesterZarebski

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.

odinserj avatar Mar 30 '18 17:03 odinserj

Thanks and Happy Easter :-).

SylwesterZarebski avatar Mar 30 '18 17:03 SylwesterZarebski

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

odinserj avatar Mar 30 '18 17:03 odinserj

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.

lucashutyler avatar Oct 07 '20 13:10 lucashutyler

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

jbennink avatar Dec 07 '22 16:12 jbennink

@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

jbennink avatar Dec 07 '22 16:12 jbennink