Question

The problem. In my Django application, users create tasks for scheduled execution. The users are quite non-technical, and it would be great if they can write conventional human-readable expressions to define when to execute certain task, such as:

  • every monday
  • every fri, wed
  • daily
  • 1, 14, 20 of each month
  • every fri; every end of month

This is inspired by Todoist. For now, only dates are necessary; no times. I've spent a couple of hours googling for a library to do that, but with no luck. I am expecting a function, say, in_range(expression, date), such that:

>>> in_range('every monday, wednesday', date(2014, 4, 28))
True

>>> in_range('every end of month', date(2014, 5, 12))
False

>>> in_range('every millenium', date(2014, 5, 8))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: unknown token "millenium".

Variants. That's what I've looked through.

  • Standard datetime library does date parsing, but not date range parsing as per above.
  • Python-dateutil - supports recurring dates via rrule, very functional, but still does not support parsing.
  • Python-crontab and Python-croniter accept standard Unix crontab syntax (and allow to specify weekdays, etc), but still such syntax is a way too technical and I'd like to avoid it if possible.
  • Arrow and Parsedatetime do not support the feature.

So, is there a Python code snippet, or a library that I missed, to do the thing? If not, I'm going to write the parser myself. Would like to release it in open source if it appears to be not too bad.

Was it helpful?

Solution

Recurrent is a library that will do natural language date parsing with support for recurring dates. It doesn't match the API you provided, but allows you to create rules that can be used with Python's datetime library.

From their Github page:

Natural language parsing of dates and recurring events

Examples

Date times

  • next tuesday
  • tomorrow
  • in an hour

Recurring events

  • on weekdays
  • every fourth of the month from jan 1 2010 to dec 25th 2020
  • each thurs until next month
  • once a year on the fourth thursday in november
  • tuesdays and thursdays at 3:15

Messy strings

  • Please schedule the meeting for every other tuesday at noon
  • Set an alarm for next tuesday at 11pm
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top