简体   繁体   中英

Converting date string in unknown format to datetime

I'm building a generic custom strToDatetime(string) function. The date string may be in some different formats. The 2 most popular alternatives seem datetime.strptime(string, format) and dateutil.parser(string) . It seems datetime.strptime() requires a format and dateutil.parser() does not, so the possible solutions seem to be:

  1. Test date strings pattern to find date string format and use datetime.strptime()
  2. Use dateutil.parser()

Is this correct? Alternative 1 (harder and may require maintenance in the future) has advantages, such as performance?

The parse() method of dateutil is very flexible and will parse almost anything you throw at it.

However, because of that flexibility, if your input is limited to a certain number of patterns, custom code that checks for those patterns then uses datetime.datetime.strptime() could easily beat it.

Since this depends entirely on the number of patterns you need to test for, the only thing you can do is measure which one will be faster for your specific usecases .

I would always go with the simplest (dateutil.parser), someone has always done the work for you and it's less likely to spit out an error at a malformed (according to your format) date.

Of course, sometimes you will want it to throw an error at a malformed, perhaps ambiguous date , and in this case you should use strptime !

Performance test:

I called parse(d) and datetime.datetime.strptime(d, f) each 100,000 times.

parse(d) took 5.62201309204 seconds
datetime.datetime.strptime(d, f) took 1.78140687943 seconds
    (where d = '11-02-1980' and f = '%m-%d-%Y')

It seems that if you know the precise date format then strptime is around 3 times faster, granted this is not a very scientific experiment but I think it gives a good indication.

So is this slight speed improvement worth the additional unnecessary complication/headache? That's up to you (but probably not).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM