Ticket #10335: 10335-tzname-locale.patch

File 10335-tzname-locale.patch, 2.1 KB (added by Armin Ronacher, 15 years ago)

improved fix that moved the encoding constant to the encodings module because it can be used in more than one location, fixed a bug in repr

  • django/utils/tzinfo.py

     
    11"Implementation of tzinfo classes for use with datetime.datetime."
    22
    3 import locale
    43import time
    54from datetime import timedelta, tzinfo
    6 from django.utils.encoding import smart_unicode
     5from django.utils.encoding import smart_unicode, smart_str, DEFAULT_LOCALE_ENCODING
    76
    8 try:
    9     DEFAULT_ENCODING = locale.getdefaultlocale()[1] or 'ascii'
    10 except:
    11     # Any problems at all determining the locale and we fallback. See #5846.
    12     DEFAULT_ENCODING = 'ascii'
    13 
    147class FixedOffset(tzinfo):
    158    "Fixed offset in minutes east from UTC."
    169    def __init__(self, offset):
     
    4134        self._tzname = self.tzname(dt)
    4235
    4336    def __repr__(self):
    44         return self._tzname
     37        return smart_str(self._tzname)
    4538
    4639    def utcoffset(self, dt):
    4740        if self._isdst(dt):
     
    5750
    5851    def tzname(self, dt):
    5952        try:
    60             return smart_unicode(time.tzname[self._isdst(dt)], DEFAULT_ENCODING)
     53            return smart_unicode(time.tzname[self._isdst(dt)],
     54                                 DEFAULT_LOCALE_ENCODING)
    6155        except UnicodeDecodeError:
    6256            return None
    6357
  • django/utils/encoding.py

     
    11import types
    22import urllib
     3import locale
    34import datetime
     5import codecs
    46
    57from django.utils.functional import Promise
    68
     
    136138        return iri
    137139    return urllib.quote(smart_str(iri), safe='/#%[]=:;$&()+,!?*')
    138140
     141
     142# The encoding of the default system locale but falls back to the
     143# given fallback encoding if the encoding is unsupported by python or could
     144# not be determined.  See tickets #10335 and #5846
     145try:
     146    DEFAULT_LOCALE_ENCODING = locale.getdefaultlocale()[1] or 'ascii'
     147    codecs.lookup(DEFAULT_LOCALE_ENCODING)
     148except:
     149    DEFAULT_LOCALE_ENCODING = 'ascii'
Back to Top