Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Parsing three-digit years using Python datetime

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading