1

I have some data that has descriptive dates (e.g., Monday before Thanksgiving, Last day in February, 4th Saturday in April) as part of describing start and end times. Some of the dates are explicit (e.g., October 31st). I want to store the descriptive and the explicit values so for any year I can then calculate when the exact dates are. I did some searching and came up short.

This feels like a common thing, and someone must have solved it. I'm also curious if these kinds of descriptive dates have a proper name.

As in the tags, my app uses Python + Django.

Thanks!

Patrick
  • 2,044
  • 1
  • 25
  • 44
  • 1
    Sounds more like a natural language processing problem to me. There is that [`timex`](https://github.com/nltk/nltk_contrib/blob/master/nltk_contrib/timex.py) nltk contrib utility that others mention in similar topics, but I don't see it works for your input strings well. `Delorean` library has the [natural language](http://delorean.readthedocs.io/en/latest/quickstart.html#natural-language) support but the other way around. – alecxe Jun 18 '16 at 21:39
  • 1
    You can also look into using external services like https://wit.ai/ or [`chrono`](http://wanasit.github.io/pages/chrono/) which may parse and interpret your "descriptive" date. – alecxe Jun 18 '16 at 21:48
  • 1
    Also see http://stackoverflow.com/questions/1495487/is-there-any-python-library-for-parsing-dates-and-times-from-a-natural-language and http://stackoverflow.com/questions/1003326/is-there-a-natural-language-parser-for-date-times-in-javascript. – alecxe Jun 18 '16 at 21:54
  • "natural language" was a key phrase there and unlocked a lot more searches. Thank you ! – Patrick Jun 18 '16 at 22:12

2 Answers2

1

If you have only a handful of reoccurring descriptive dates, the easiest thing to do would be to create a dictionary that can translate them to the explicit dates you want whenever they pop up in your data.

If you have arbitrary descriptive dates or a large number of them, however, it seems that, as was discussed in the comments, NLP is the way to go.

0

This is the answer: Maya

>>> import maya
>>> weird_day=maya.when('3rd Saturday in June')
>>> weird_day.rfc2822()
'Sun, 03 Jun 2018 00:00:00 GMT'
>>> weird_day.datetime(to_timezone='US/Pacific')
datetime.datetime(2018, 6, 2, 17, 0, tzinfo=<DstTzInfo 'US/Pacific' PDT-1 day, 17:00:00 DST>)
Patrick
  • 2,044
  • 1
  • 25
  • 44