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.)
--
Arfrever Frehtes Taifersar Arahesis
--- date.py
+++ date.py
@@ -22,6 +22,7 @@
import math
import re
+import sys
from locale import getpreferredencoding
from datetime import date, time, datetime, timedelta
from time import strptime as time_strptime
@@ -279,29 +280,37 @@
def ustrftime(somedate, fmt='%Y-%m-%d'):
"""like strftime, but returns a unicode string instead of an encoded
- string which' may be problematic with localized date.
+ string which may be problematic with localized date.
- encoding is guessed by locale.getpreferredencoding()
+ When using Python 2, encoding is guessed by locale.getpreferredencoding().
"""
- encoding = getpreferredencoding(do_setlocale=False) or 'UTF-8'
- try:
- return unicode(somedate.strftime(str(fmt)), encoding)
- except ValueError, exc:
- if somedate.year >= 1900:
- raise
- # datetime is not happy with dates before 1900
- # we try to work around this, assuming a simple
- # format string
- fields = {'Y': somedate.year,
- 'm': somedate.month,
- 'd': somedate.day,
- }
- if isinstance(somedate, datetime):
- fields.update({'H': somedate.hour,
- 'M': somedate.minute,
- 'S': somedate.second})
- fmt = re.sub('%([YmdHMS])', r'%(\1)02d', fmt)
- return unicode(fmt) % fields
+ if sys.version_info >= (3, 3):
+ # datetime.date.strftime() supports dates since year 1 in Python >=3.3.
+ return somedate.strftime(fmt)
+ else:
+ if sys.version_info < (3, 0):
+ encoding = getpreferredencoding(do_setlocale=False) or 'UTF-8'
+ try:
+ if sys.version_info < (3, 0):
+ return unicode(somedate.strftime(str(fmt)), encoding)
+ else:
+ return somedate.strftime(fmt)
+ except ValueError, exc:
+ if somedate.year >= 1900:
+ raise
+ # datetime is not happy with dates before 1900
+ # we try to work around this, assuming a simple
+ # format string
+ fields = {'Y': somedate.year,
+ 'm': somedate.month,
+ 'd': somedate.day,
+ }
+ if isinstance(somedate, datetime):
+ fields.update({'H': somedate.hour,
+ 'M': somedate.minute,
+ 'S': somedate.second})
+ fmt = re.sub('%([YmdHMS])', r'%(\1)02d', fmt)
+ return unicode(fmt) % fields
def utcdatetime(dt):
if dt.tzinfo is None:
signature.asc
Description: This is a digitally signed message part.
_______________________________________________ Python-Projects mailing list [email protected] http://lists.logilab.org/mailman/listinfo/python-projects
