Title: datetime strftime with %Y and 2 digit years
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8
Status: closed Resolution: duplicate
Dependencies: Superseder: datetime.strftime("%Y") not consistent for years < 1000
View: 13305
Assigned To: Nosy List: belopolsky, laurent chriqui, p-ganssle
Priority: normal Keywords:

Created on 2020-06-08 10:00 by laurent chriqui, last changed 2020-06-08 17:07 by p-ganssle. This issue is now closed.

Messages (2)
msg370971 - (view) Author: laurent chriqui (laurent chriqui) Date: 2020-06-08 10:00
When you use strftime with a 2 digit year i.e. '32' it outputs a '32' instead of '0032'. 

This prevents parsing the string again with the same format through strftime.

import datetime

date_str=datetime.datetime.strftime(date, datetime_format)
datetime.datetime.strptime(date_str, datetime_format)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/usr/lib/python3.8/", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data '32' does not match format '%Y'
msg371019 - (view) Author: Paul Ganssle (p-ganssle) * (Python committer) Date: 2020-06-08 17:07
This is a duplicate of bpo-13305 and is due to platform-specific implementations of %Y. On Linux, `strftime()` does not zero-pad to 4, and if you need to represent years <1000, you should use "%4Y" to zero-pad the output.

I think the ideal resolution would be a cross-platform implementation of strftime and strptime that does not rely on the system symbols, but that is a pretty large and overwhelming project.
Date User Action Args
2020-06-08 17:07:20p-gansslesetstatus: open -> closed
superseder: datetime.strftime("%Y") not consistent for years < 1000
messages: + msg371019

resolution: duplicate
stage: resolved
2020-06-08 14:48:20xtreaksetnosy: + belopolsky, p-ganssle
2020-06-08 10:00:33laurent chriquicreate