views:

120

answers:

3

I did some searching but haven't landed anything that looks useful yet but I am wondering if anyone knows of something (tool,lib etc) that can parse English phrases and translate them into a cron string.

For example: Every Tuesday at 15:00 converts to 0 15 * * 2

It seems like something that would have lots of gotchas and it would be preferable to benefit from someone elses work. You see it in a few nice sites/apps that can work out what you mean from a simple phrase rather than having some hideous user interface.

Thanks, Tom

A: 

Depending on how flexible you need it to be, and how willing to roll up your own sleeves you are, you could define a simple grammar for this.

Every would be a quantifier. You may need others but I can't think of any. Valid syntax might be:

Every (day-spec) AT (time)

Where day-spec could be a literal day (ie. Monday) or be a day of the month (ie. 30th Day) or some other syntax (I'd suggest fortnights but I'm not sure if Cron can represent those well).

Time could be specified using either 24 hour (16:00) or 12 hour (4:00pm) format.

Another syntax that you might want is: Every (frequency) From (time) where frequency is basically (quantity) (unit) (ie. 10 Minutes). The from time enables you to set an offset (eg. Every 30 Minutes From 01:10am).

You'd probably need to sit down and figure out these details a bit more. But a rigid grammar could be implemented relatively easily using recursive descent.

Adam Luchjenbroers
Defining a grammar for this would be over the top. There's nothing recursive about this grammar. Making a small statemachine is the fastest route
Toad
@reinier It wouldn't be SO if there wasn't the "roll your own hammer" for the "how to crack a walnut?" question. :)
Tom Duckering
+1  A: 

For Ruby there's "Whenever" which might provide a starting point: It translates quasi-english (actually it's valid Ruby) into cron strings.

Beat Bolli
A: 

Hmm, about those gotchas... How about also writing one that translates the cron params back to English? That way you can see if the parser "understood" you.

gbarry
I saw a lot of stuff that translates cron to English whilst searching and had the same thought. Obviously you'd not always get the same English back out the other end since there's a many-to-one from English to cron. Still a nice idea. Rather reminds me of the fun that can be had translating phrases to and from foreign languages using google translate.
Tom Duckering