]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Revert change to use time.strftime (from 3.0).
authorBen Darnell <ben@bendarnell.com>
Fri, 24 May 2013 04:41:23 +0000 (00:41 -0400)
committerBen Darnell <ben@bendarnell.com>
Fri, 24 May 2013 04:48:44 +0000 (00:48 -0400)
time.strftime is influenced by the user's locale (if one is set with
locale.setlocale), so it's not what we want.  Go back to the (slower)
email.utils functions.

Fixes #800.

tornado/httputil.py
tornado/test/runtests.py
tornado/test/web_test.py
tornado/web.py
tox.ini

index 36d7dc8faed63eb7469fc97c5e0b6cee65b461b3..3d17b6107f5cafd1583013d03857d5344fd78b2c 100644 (file)
 
 from __future__ import absolute_import, division, print_function, with_statement
 
+import calendar
 import collections
 import datetime
+import email.utils
 import numbers
 import re
 import time
@@ -382,15 +384,15 @@ def format_timestamp(ts):
     >>> format_timestamp(1359312200)
     'Sun, 27 Jan 2013 18:43:20 GMT'
     """
-    if isinstance(ts, (tuple, time.struct_time)):
+    if isinstance(ts, numbers.Real):
         pass
+    elif isinstance(ts, (tuple, time.struct_time)):
+        ts = calendar.timegm(ts)
     elif isinstance(ts, datetime.datetime):
-        ts = ts.utctimetuple()
-    elif isinstance(ts, numbers.Real):
-        ts = time.gmtime(ts)
+        ts = calendar.timegm(ts.utctimetuple())
     else:
         raise TypeError("unknown timestamp type: %r" % ts)
-    return time.strftime("%a, %d %b %Y %H:%M:%S GMT", ts)
+    return email.utils.formatdate(ts, usegmt=True)
 
 # _parseparam and _parse_header are copied and modified from python2.7's cgi.py
 # The original 2.7 version of this code did not correctly support some
index 22cda522d0abbb3b64d754ec964d8e4cabcdd155..1f4c08c96759087d42f0533e28b6fd2e65557a71 100644 (file)
@@ -2,6 +2,7 @@
 
 from __future__ import absolute_import, division, print_function, with_statement
 import gc
+import locale  # system locale module, not tornado.locale
 import logging
 import operator
 import textwrap
@@ -95,6 +96,8 @@ if __name__ == '__main__':
            "e.g. DEBUG_STATS or DEBUG_COLLECTABLE,DEBUG_OBJECTS",
            callback=lambda values: gc.set_debug(
             reduce(operator.or_, (getattr(gc, v) for v in values))))
+    define('locale', type=str, default=None,
+           callback=lambda x: locale.setlocale(locale.LC_ALL, x))
 
     def configure_ioloop():
         kwargs = {}
index 74eec11e964d08702ec80ff013442416ee714a74..d3884393693e99e07caaccdf26a250c61c315d36 100644 (file)
@@ -13,6 +13,7 @@ from tornado.web import RequestHandler, authenticated, Application, asynchronous
 
 import binascii
 import datetime
+import email.utils
 import logging
 import os
 import re
@@ -1160,8 +1161,8 @@ class DateHeaderTest(SimpleHandlerTestCase):
 
     def test_date_header(self):
         response = self.fetch('/')
-        header_date = datetime.datetime.strptime(response.headers['Date'],
-                                                 "%a, %d %b %Y %H:%M:%S GMT")
+        header_date = datetime.datetime(
+            *email.utils.parsedate(response.headers['Date'])[:6])
         self.assertTrue(header_date - datetime.datetime.utcnow() <
                         datetime.timedelta(seconds=2))
 
index 2ede8d32deeb81a5ea861c6f7e26e1b6e8275285..f670d8aa727a62e62912e32fac2e691db90cb7c3 100644 (file)
@@ -239,7 +239,7 @@ class RequestHandler(object):
         self._headers = httputil.HTTPHeaders({
             "Server": "TornadoServer/%s" % tornado.version,
             "Content-Type": "text/html; charset=UTF-8",
-            "Date": httputil.format_timestamp(time.gmtime()),
+            "Date": httputil.format_timestamp(time.time()),
         })
         self.set_default_headers()
         if (not self.request.supports_http_1_1() and
diff --git a/tox.ini b/tox.ini
index 95ab47af7624b5d15fd375ae41312bec212b0100..3d143c3ae41f1ae39edfddc63d371c3d20ba74a4 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -11,7 +11,7 @@
 [tox]
 # "-full" variants include optional dependencies, to ensure
 # that things work both in a bare install and with all the extras.
-envlist = py27-full, py27-curl, py32-full, pypy, py26, py26-full, py27, py32, py32-utf8, py33, py27-opt, py32-opt, pypy-full, py27-select, py27-monotonic, py33-monotonic, py27-twisted, py27-threadedresolver, py27-twistedresolver, py27-twistedlayered, py27-caresresolver, py32-caresresolver, py27-docs
+envlist = py27-full, py27-curl, py32-full, pypy, py26, py26-full, py27, py32, py32-utf8, py33, py27-opt, py32-opt, pypy-full, py27-select, py27-monotonic, py33-monotonic, py27-twisted, py27-threadedresolver, py27-twistedresolver, py27-twistedlayered, py27-caresresolver, py32-caresresolver, py27-locale, py27-docs
 [testenv]
 commands = python -m tornado.test.runtests {posargs:}
 
@@ -138,6 +138,10 @@ deps =
      twisted
 commands = python -m tornado.test.runtests --resolver=tornado.platform.caresresolver.CaresResolver {posargs:}
 
+[testenv:py27-locale]
+basepython = python2.7
+commands = python -m tornado.test.runtests --locale=zh_TW {posargs:}
+
 [testenv:pypy-full]
 # This configuration works with pypy 1.9.  pycurl installs ok but
 # curl_httpclient doesn't work.  Twisted works most of the time, but