Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing human-readable recurring dates in Python

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.

like image 937
Anatoly Scherbakov Avatar asked Apr 26 '14 15:04

Anatoly Scherbakov


People also ask

How do you parse a date format in Python?

Python has a built-in method to parse dates, strptime . This example takes the string “2020–01–01 14:00” and parses it to a datetime object. The documentation for strptime provides a great overview of all format-string options.

What is date parser in Python?

dateparser provides modules to easily parse localized dates in almost any string formats commonly found on web pages.

Can Python Recognise dates?

A date in Python is not a data type of its own, but we can import a module named datetime to work with dates as date objects.


1 Answers

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
like image 114
Darrick Herwehe Avatar answered Oct 02 '22 17:10

Darrick Herwehe