Home > Blockchain >  Can Python's strptime handle Python's logging time format?
Can Python's strptime handle Python's logging time format?

Time:12-01

By default, Python's logging module uses a special format for times, which includes milliseconds: 2003-01-23 00:29:50,411.

Notably, strftime and strptime don't have a standard "milliseconds" specifier (so logging first prints everything else with strftime and then inserts the milliseconds separately). This means there's no obvious way to parse these strings using the standard library.

However, everything seems to work fine when I use strptime with the %f (microseconds) specifier: %Y-%m-%d %H:%M:%S,%f. While %f in strftime prints a six-digit number, in strptime it's apparently happy to take a three-digit number instead, and assume the last three digits are zeroes.

My question is—is this standard/documented behavior? Can I rely on this to keep working, or is it liable to break unexpectedly in new versions? I haven't been able to find anything about this in the Python docs (or man strptime, which doesn't even mention %f), but this seems like a common enough use case that I'd be surprised if nobody's needed it before.

I could also append three zeroes to the time string before passing it to strptime, but that's hacky enough that I'd prefer not to do it unless necessary.

CodePudding user response:

This is in fact standard—for Python, at least. (%f is not a C-standard directive, which is why I couldn't find it in man strptime.) It's in the notes under Technical Detail:

When used with the strptime() method, the %f directive accepts from one to six digits and zero pads on the right. %f is an extension to the set of format characters in the C standard (but implemented separately in datetime objects, and therefore always available).

Thanks to Kelly Bundy for pointing me to this in the comments!

CodePudding user response:

As I said in my comments, I think the best (and safest) way to handle such strings is by using datetime.fromisoformat() and datetime.isoformat. This way, you don't have to care about messing with format templates ever:

>>> from datetime import datetime
>>> datetime.fromisoformat('2003-01-23 00:29:50,411')
datetime.datetime(2003, 1, 23, 0, 29, 50, 411000)
>>>
  • Related