Home > Enterprise >  Parsing three-digit years using datetime
Parsing three-digit years using datetime

Time:02-15

I am seeing a strange issue parsing date-time strings with dates containing three-digit years with Python 3.x. Here's an example:

from datetime import datetime

format = '%Y-%m-%dT%H:%M:%SZ'
dt = datetime.strptime('0699-02-02T15:11:43Z', format)
print(dt) # datetime.datetime(699, 2, 2, 15, 11, 43)
print(dt.strftime(format)) # '699-02-02T15:11:43Z'
print(datetime.strptime(dt.strftime(format), format))

I expected this to output datetime.datetime(699, 2, 2, 15, 11, 43), but it produces an error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/usr/local/lib/python3.8/_strptime.py", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data '699-02-02T15:11:43Z' does not match format '%Y-%m-%dT%H:%M:%SZ'

According to the documentation, using %Y should produce a padded four-digit year, but this is not the output of strftime. Moreover, the output that I do get cannot be parsed back to the original datetime as the error above shows.

What could I be missing?

Version: I am running via the python:3.8 Docker image, but I have also tried python:3.6 and python:3.10.

CodePudding user response:

Seems like it's a reported bug on Python.

I'm using Python 3.10 on Windows and the output works as expected:

>> print(datetime.strptime(dt.strftime(format), format))
0699-02-02 15:11:43

A possible solution (hack): add the missing leading zeros to your string:

my_date = dt.strftime(format)
my_date = my_date.zfill(20)
  • Related