dateutil functions for Datasette
Install this plugin in the same environment as Datasette.
$ datasette install datasette-dateutil
This function adds custom SQL functions that expose functionality from the dateutil Python library.
Once installed, the following SQL functions become available:
dateutil_parse(text)
- returns an ISO8601 date string parsed from the text, ornull
if the input could not be parsed.dateutil_parse("10 october 2020 3pm")
returns2020-10-10T15:00:00
.dateutil_parse_fuzzy(text)
- same asdateutil_parse()
but this also works against strings that contain a date somewhere within them - that date will be returned, ornull
if no dates could be found.dateutil_parse_fuzzy("This is due 10 september")
returns2020-09-10T00:00:00
(but will start returning the 2021 version of that if the year is 2021).dateutil_easter(year)
- returns the date for Easter in that year, for exampledateutil_easter("2020")
returns2020-04-12
.
The dateutil_parse()
and dateutil_parse_fuzzy()
functions both follow the American convention of assuming that 1/2/2020
lists the month first, evaluating this example to the 2nd of January.
If you want to assume that the day comes first, use these two functions instead:
dateutil_parse_dayfirst(text)
dateutil_parse_fuzzy_dayfirst(text)
To set up this plugin locally, first checkout the code. Then create a new virtual environment:
cd datasette-dateutil
python3 -mvenv venv
source venv/bin/activate
Or if you are using pipenv
:
pipenv shell
Now install the dependencies and tests:
pip install -e '.[test]'
To run the tests:
pytest