arrow-py / arrow

🏹 Better dates & times for Python
https://arrow.readthedocs.io
Apache License 2.0
8.71k stars 673 forks source link

Hope to support an api like auto_parse(time_str) #838

Closed cnkailyn closed 4 years ago

cnkailyn commented 4 years ago

Feature Request

Hope to support an api like auto_parse(time_str)

when i use arrow in web spider project, i hava to parse the datetime string to a datetime object, and do other handle. but the datetime string always like '2018-05-14', '2018-05-4', '2018-05-14', 'Mar, 05, 2020', 'February 2020', '2020-08-17 00:20'...,so i hope to have an api like auto_parse(time_str) to parse out all kinds of time strings.

jadchaar commented 4 years ago

Hi @cnkailyn, an automatic parsing function like this is hard to create since there are a number of ambiguous date formats. Calling arrow.get() with a single string argument only works with ISO 8601 date formats (and a few other unambiguous combinations).

You can, however, pass a list of formats as a second argument. Arrow will then iterate through these formats and try each one to see which one works. If you are able to generate a comprehensive list of date formats, then you can put them in a list and pass them in like this: arrow.get("DATE STRING", ["FORMAT 1", "FORMAT 2"]). Does this cover your use case?

cnkailyn commented 4 years ago

thanks, it's works although not smart.

jadchaar commented 4 years ago

I wish there was a better solution to this, but with web scraping especially, it is hard to create a large database of all the possible date and time formats that can be used. That is something we need to delegate to users to prevent bloating the package.