kcron-common icon indicating copy to clipboard operation
kcron-common copied to clipboard

Accessing information of parsed expression using `Cron.parseAndBuild`

Open axel7083 opened this issue 5 months ago • 3 comments

Description

Hey! I experimented a bit with this library, and I have a few question/remark.

When parsing an expression using Cron.parseAndBuild I would like to have some information about it, for example, which day of the week this is running on? (Any, from X to Y, only X, X and Y etc.), same for other properties.

Is this possible through this library?


I am also a bit confused about the types, when I use Cron.parseAndBuild I get a Builder<LocalDateTime, CronLocalDateTime, CronLocalDateTimeProvider> which is not really what I would expect, and I am using a parse & build function.


At first I was using cardinal for week day (E.g. * * * * 0,6 - At every minute on Sunday and Saturday.) but this library seems to be using 1 to 7. I switched to string values (MON etc.) and it works fine.


Thanks for your amazing work 👍

axel7083 avatar Sep 06 '25 14:09 axel7083

Hello @axel7083 !

Builder can return expression as a string. It's not so complicated to add posibility to return each part separatly, but I don't see any use case for it yet.


Yes Cron.parseAndBuild return a Builder instance. The Builder is a main part of the library. I didn't see any reason to return from the Builder any CronResult or something similar. Regarding generic parameters - Builder<LocalDateTime, CronLocalDateTime, CronLocalDateTimeProvider> it's a default implementation based on KotlinX DateTime library. You can implement your own provider with any time library you wish. For example, I experemented with islandtime library - all worked like a charm.


I implemented the library in ISO 8601 standard. Based on it week days are - 1 - Monday...7 - Sunday. You can specify first day of the week into settings block of parseAndBuild method. But it's still will be calculated from 1 and not from 0.


Thank you! Feel free to contribute!

Scogun avatar Sep 07 '25 08:09 Scogun

Builder can return expression as a string. It's not so complicated to add possibility to return each part separatly, but I don't see any use case for it yet.

My use case is the following, through a simple interface the user can define a repeating event, and I am storing this as a Cron expression. Later on the user should be able to edit it, and I want to be able to populate the fields back from the existing cron expression.

Therefore I need to know for each the part, the format and the values


Thanks for all the clarifications !

axel7083 avatar Sep 07 '25 10:09 axel7083

There is Compose UI Builder for it. If you need other technology - you can follow the implementation. Again, feel free to contribute any native UI Builder.

Scogun avatar Sep 07 '25 11:09 Scogun