“Natural Language” Time Periods in Python

Mulling over a search feed that includes date range limits, I had a quick look for a python library that includes “natural language” functions for describing different date ranges. Not finding anything offhand, I popped some quick starter-for-ten functions up at this gist, which should also be embedded below.

It includes things like today(), tomorrow(), last_week(), later_this_month() and so on.

If you know of a “proper” library that does this, please let me know via the comments…

UPDATE: code repo here: https://github.com/psychemedia/python-natural-time-periods

SEE ALSO: https://github.com/DavidAmison/natural_time

PS more handy fragments:

#Get month and year between two dates
import datetime
from dateutil.rrule import rrule, MONTHLY

strt_dt = datetime.date(2015,4,1)
end_dt = datetime.date(2016,10,1)

dates = ['_'.join([dt.strftime('%B').lower(), dt.strftime('%Y')]) for dt in rrule(MONTHLY, dtstart=strt_dt, until=end_dt)]

Author: Tony Hirst

I'm a Senior Lecturer at The Open University, with an interest in #opendata policy and practice, as well as general web tinkering...

One thought on ““Natural Language” Time Periods in Python”

Comments are closed.