logilab-common #82161 python3: logilab.common.date.ustrftime() is incompatible [done]

From: Arfrever Frehtes Taifersar Arahesis <arfrever.fta@gmail.com>
Date: Fri, 28 Oct 2011 17:13:15 +0200
To: Logilab Python Projects <python-projects@lists.logilab.org>

logilab.common.date.ustrftime() is incompatible with Python 3:

$ python2.7 -c 'import datetime, logilab.common.date; print(repr(logilab.common.date.ustrftime(datetime.date(2011, 10, 28))))'
u'2011-10-28'
$ python3.2 -c 'import datetime, logilab.common.date; print(repr(logilab.common.date.ustrftime(datetime.date(2011, 10, 28))))'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib64/python3.2/site-packages/logilab/common/date.py", line 288, in ustrftime
return str(somedate.strftime(str(fmt)), encoding)
TypeError: decoding str is not supported

datetime.date.strftime() already returns str instead of bytes in Python 3, so decoding is not needed.

logilab.common.date.ustrftime() tries to support dates before year 1900.
datetime.date.strftime() supports dates since year 1000 in Python 3.2 and dates since year 1 in Python 3.3:

$ python3.1 -c 'import datetime; print(repr(datetime.date(1, 1, 1).strftime("%Y-%m-%d")))'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ValueError: year=1 is before 1900; the datetime strftime() methods require year >= 1900
$ python3.2 -c 'import datetime; print(repr(datetime.date(1, 1, 1).strftime("%Y-%m-%d")))'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ValueError: year=1 is before 1000; the datetime strftime() methods require year >= 1000
$ python3.2 -c 'import datetime; print(repr(datetime.date(1000, 1, 1).strftime("%Y-%m-%d")))'
'1000-01-01'
$ python3.3 -c 'import datetime; print(repr(datetime.date(1, 1, 1).strftime("%Y-%m-%d")))'
'1-01-01'

I'm attaching the patch, which fixes logilab.common.date.ustrftime().
This patch fixes errors in test_ticks2datetime_before_1900() and test_ustrftime_before_1900() in
unittest_date.py with Python 3.2. (There are no other errors in unittest_date.py with Python 3.2.)

prioritynormal
typebug
done in0.58.3
load left0.000
closed by#29e8e0b50e57 fix date.ustrftime for python3, closes #82161