summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSylvain Th?nault <sylvain.thenault@logilab.fr>2012-08-29 15:21:28 +0200
committerSylvain Th?nault <sylvain.thenault@logilab.fr>2012-08-29 15:21:28 +0200
commitd1d98bfbb443f23f06e20ee7cdb7d62c303aeb2c (patch)
tree0dad38022cbc60deffd202ac9859f93726b94ef5
parent586f9e9c3d1802862fcef97259d5662b3c8f9744 (diff)
downloadlogilab-common-d1d98bfbb443f23f06e20ee7cdb7d62c303aeb2c.tar.gz
fix date.ustrftime for python3, closes #82161
-rw-r--r--ChangeLog4
-rw-r--r--date.py52
2 files changed, 34 insertions, 22 deletions
diff --git a/ChangeLog b/ChangeLog
index 2b19771..5bb288b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,10 @@
ChangeLog for logilab.common
============================
+ --
+ * fix logilab.common.date.ustrftime() for python3 (closes #82161,
+ patch by Arfrever Frehtes Taifersar Arahesis)
+
2012-07-30 -- 0.58.2
* modutils: fixes (closes #100757 and #100935)
diff --git a/date.py b/date.py
index b069a6f..7af7aa6 100644
--- a/date.py
+++ b/date.py
@@ -1,4 +1,4 @@
-# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of logilab-common.
@@ -22,6 +22,7 @@ __docformat__ = "restructuredtext en"
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,36 @@ def last_day(somedate):
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:
+ try:
+ if sys.version_info < (3, 0):
+ encoding = getpreferredencoding(do_setlocale=False) or 'UTF-8'
+ 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: