7

Many common human-readable datetimes can be parsed using datetime.strptime(), for example:

'15 March 2019 13:00'

And I know of several Python packages that parse "fuzzy" human-readable datetimes into Python datetime objects, such as dateparser and parsedatetime, which can parse strings such as:

'yesterday'
'next week'

But is there a Python library that can parse common human-readable datetime durations into start and end datetime objects. For example:

'5 March 2019 13:00-18:30'
'5 March 2019 1:00-5:30pm'
'15-18 March 2019'
'2nd to 22nd March 2019'

into:

[datetime.datetime(2019, 3, 5, 13, 0), datetime.datetime(2019, 3, 5, 18, 30)]
[datetime.datetime(2019, 3, 5, 13, 0), datetime.datetime(2019, 3, 5, 18, 30)]
[datetime.datetime(2019, 3, 15, 0, 0), datetime.datetime(2019, 3, 18, 0, 0)]
[datetime.datetime(2019, 3, 2, 0, 0), datetime.datetime(2019, 3, 22, 0, 0)]

or take a string like:

'15-17 March 2019'

and return a list of inclusive dates?

[datetime.datetime(2019, 3, 15, 0, 0), datetime.datetime(2019, 3, 16, 0, 0), datetime.datetime(2019, 3, 17, 0, 0)]

Note that I'm interested in parsing specific dates and times rather than non-calendar-based durations such as '2 days' or 'three weeks'.

birophilo
  • 912
  • 10
  • 20
  • 1
    There're already a rich collection of such libraries in this [post](https://stackoverflow.com/questions/466345/converting-string-into-datetime). – Gusi Gao Mar 03 '19 at 01:41
  • 3
    Of those, only one - timestring - parses calendar durations, as far as I can see. And that is an archived project with very limited range parsing ability - for example "15-18 March 2019" returns the whole month, the same as "March 2019". None of those libraries, nor the standard library, address the use cases above - although I am happy to be proven wrong. – birophilo Mar 03 '19 at 02:26
  • 1
    Somebody else suggested [datefinder](https://github.com/akoumjian/datefinder). Thanks for the suggestion - I tried it with the above strings and it can handle '2nd to 22nd March 2019' but not the others. – birophilo Mar 03 '19 at 02:52
  • 1
    Have you looked at this: https://dateutil.readthedocs.io/en/stable – Life is complex Mar 03 '19 at 02:54
  • 1
    I'm interested in this question, because I see this capability being useful in the future, so I was wondering if you have made any progress? – Life is complex Mar 09 '19 at 18:58
  • 1
    Hi sorry I didn't reply. I've used dateutil before. Its `parser` is great for single datetimes, and it also has `rrule` for recurring rules, to generate a list of, say, 7 sequential dates. But AFAIK it doesn't have a feature that parses human-readable date durations into rrules. I think the feature would be best as an additional module to dateutil or an extension of datefinder's regex. I might even have a go at that myself sometime, but this was just for a side project and I haven't done any more on it yet. – birophilo Mar 09 '19 at 20:00
  • 1
    I helped answer a similar question: https://stackoverflow.com/a/46222835/4985733 – Martin Evans Jul 12 '19 at 13:34

0 Answers0