[Python-checkins] cpython: Issue #747320: Use email.utils.formatdate() to avoid code duplication

berker.peksag python-checkins at python.org
Mon Mar 14 00:05:18 EDT 2016


https://hg.python.org/cpython/rev/ee64faffd46a
changeset:   100520:ee64faffd46a
user:        Berker Peksag <berker.peksag at gmail.com>
date:        Mon Mar 14 06:06:03 2016 +0200
summary:
  Issue #747320: Use email.utils.formatdate() to avoid code duplication
in BaseHTTPRequestHandler

Initial patch by karlcow.

files:
  Lib/http/server.py           |   8 ++------
  Lib/test/test_httpservers.py |  14 ++++++++++++++
  2 files changed, 16 insertions(+), 6 deletions(-)


diff --git a/Lib/http/server.py b/Lib/http/server.py
--- a/Lib/http/server.py
+++ b/Lib/http/server.py
@@ -87,6 +87,7 @@
     "SimpleHTTPRequestHandler", "CGIHTTPRequestHandler",
 ]
 
+import email.utils
 import html
 import http.client
 import io
@@ -566,12 +567,7 @@
         """Return the current date and time formatted for a message header."""
         if timestamp is None:
             timestamp = time.time()
-        year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp)
-        s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
-                self.weekdayname[wd],
-                day, self.monthname[month], year,
-                hh, mm, ss)
-        return s
+        return email.utils.formatdate(timestamp, usegmt=True)
 
     def log_date_time_string(self):
         """Return the current time formatted for logging."""
diff --git a/Lib/test/test_httpservers.py b/Lib/test/test_httpservers.py
--- a/Lib/test/test_httpservers.py
+++ b/Lib/test/test_httpservers.py
@@ -17,6 +17,7 @@
 import html
 import http.client
 import tempfile
+import time
 from io import BytesIO
 
 import unittest
@@ -873,6 +874,19 @@
         self.handler.handle()
         self.assertRaises(StopIteration, next, close_values)
 
+    def test_date_time_string(self):
+        now = time.time()
+        # this is the old code that formats the timestamp
+        year, month, day, hh, mm, ss, wd, y, z = time.gmtime(now)
+        expected = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
+            self.handler.weekdayname[wd],
+            day,
+            self.handler.monthname[month],
+            year, hh, mm, ss
+        )
+        self.assertEqual(self.handler.date_time_string(timestamp=now), expected)
+
+
 class SimpleHTTPRequestHandlerTestCase(unittest.TestCase):
     """ Test url parsing """
     def setUp(self):

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list