summaryrefslogtreecommitdiffstats
path: root/Lib/email
diff options
context:
space:
mode:
authorAnthony Baxter <anthonybaxter@gmail.com>2004-10-11 13:53:08 (GMT)
committerAnthony Baxter <anthonybaxter@gmail.com>2004-10-11 13:53:08 (GMT)
commit3dd9e461618b0964312fa3c649cf03c7bfe27827 (patch)
treeb8c98e967b38f936410f4e3fddda413bc9c8db3d /Lib/email
parent7f468f29f49786988e4f1a005174578fc83c7eb9 (diff)
downloadcpython-3dd9e461618b0964312fa3c649cf03c7bfe27827.zip
cpython-3dd9e461618b0964312fa3c649cf03c7bfe27827.tar.gz
cpython-3dd9e461618b0964312fa3c649cf03c7bfe27827.tar.bz2
Added a usegmt flag to email.Utils.formatdate - this allows it to be
used to replace rfc822.formatdate for protocols like HTTP (where 'GMT' must be the timezone string).
Diffstat (limited to 'Lib/email')
-rw-r--r--Lib/email/Utils.py11
-rw-r--r--Lib/email/test/test_email.py9
2 files changed, 18 insertions, 2 deletions
diff --git a/Lib/email/Utils.py b/Lib/email/Utils.py
index e786d26..f210eec 100644
--- a/Lib/email/Utils.py
+++ b/Lib/email/Utils.py
@@ -103,7 +103,7 @@ ecre = re.compile(r'''
-def formatdate(timeval=None, localtime=False):
+def formatdate(timeval=None, localtime=False, usegmt=False):
"""Returns a date string as specified by RFC 2822, e.g.:
Fri, 09 Nov 2001 01:08:47 -0000
@@ -114,6 +114,10 @@ def formatdate(timeval=None, localtime=False):
Optional localtime is a flag that when True, interprets timeval, and
returns a date relative to the local timezone instead of UTC, properly
taking daylight savings time into account.
+
+ Optional argument usegmt means that the timezone is written out as
+ an ascii string, not numeric one (so "GMT" instead of "+0000"). This
+ is needed for HTTP, and is only used when localtime==False.
"""
# Note: we cannot use strftime() because that honors the locale and RFC
# 2822 requires that day and month names be the English abbreviations.
@@ -138,7 +142,10 @@ def formatdate(timeval=None, localtime=False):
else:
now = time.gmtime(timeval)
# Timezone offset is always -0000
- zone = '-0000'
+ if usegmt:
+ zone = 'GMT'
+ else:
+ zone = '-0000'
return '%s, %02d %s %04d %02d:%02d:%02d %s' % (
['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'][now[6]],
now[2],
diff --git a/Lib/email/test/test_email.py b/Lib/email/test/test_email.py
index f2aa8d7..745260e 100644
--- a/Lib/email/test/test_email.py
+++ b/Lib/email/test/test_email.py
@@ -2037,6 +2037,15 @@ class TestMiscellaneous(unittest.TestCase):
Utils.parsedate(Utils.formatdate(now, localtime=True))[:6],
time.localtime(now)[:6])
+ def test_formatdate_usegmt(self):
+ now = time.time()
+ self.assertEqual(
+ Utils.formatdate(now, localtime=False),
+ time.strftime('%a, %d %b %Y %H:%M:%S -0000', time.gmtime(now)))
+ self.assertEqual(
+ Utils.formatdate(now, localtime=False, usegmt=True),
+ time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(now)))
+
def test_parsedate_none(self):
self.assertEqual(Utils.parsedate(''), None)