]> granicus.if.org Git - python/commitdiff
formatdate(): An implementation to replace the one borrowed from
authorBarry Warsaw <barry@python.org>
Fri, 9 Nov 2001 16:59:56 +0000 (16:59 +0000)
committerBarry Warsaw <barry@python.org>
Fri, 9 Nov 2001 16:59:56 +0000 (16:59 +0000)
rfc822.py.  The old rfc822.formatdate() produced date strings using
obsolete syntax.  The new version produces the preferred RFC 2822
dates.

Also, an optional argument `localtime' is added, which if true,
produces a date relative to the local timezone, with daylight savings
time properly taken into account.

Lib/email/Utils.py

index 6bbf2d35cb14657630f08ffe8ba10c1bc7e409ab..237b68699c4bbecfbff2cb2c0d19d3080727ef1e 100644 (file)
@@ -9,7 +9,7 @@ import re
 from rfc822 import unquote, quote, parseaddr
 from rfc822 import dump_address_pair
 from rfc822 import AddrlistClass as _AddrlistClass
-from rfc822 import parsedate_tz, parsedate, mktime_tz, formatdate
+from rfc822 import parsedate_tz, parsedate, mktime_tz
 
 from quopri import decodestring as _qdecode
 import base64
@@ -102,3 +102,39 @@ def encode(s, charset='iso-8859-1', encoding='q'):
     else:
         raise ValueError, 'Illegal encoding code: ' + encoding
     return '=?%s?%s?%s?=' % (charset.lower(), encoding.lower(), estr)
+
+
+\f
+def formatdate(timeval=None, localtime=0):
+    """Returns a formatted time as specified by RFC 2822, e.g.:
+
+    Fri, 09 Nov 2001 01:08:47 -0000
+
+    Optional timeval if given is a float time as accepted by localtime() or
+    gmtime().  Optional localtime is a flag that when true, interprets and
+    returns a time relative to the local timezone instead of UTC.
+    """
+    # Note: we cannot use strftime() because that honors the locale and RFC
+    # 2822 requires that day and month names be the English abbreviations.
+    if timeval is None:
+        timeval = time.time()
+    if localtime:
+        now = time.localtime(timeval)
+        # Calculate timezone offset, based on whether the local zone has
+        # daylight savings time, and whether DST is in effect.
+        if time.daylight and now[-1]:
+            offset = time.altzone
+        else:
+            offset = time.timezone
+        zone = '%+03d%02d' % (offset / -3600, offset % 60)
+    else:
+        now = time.gmtime(timeval)
+        # Timezone offset is always -0000
+        zone = '-0000'
+    return '%s, %02d %s %04d %02d:%02d:%02d %s' % (
+        ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'][now[6]],
+        now[2],
+        ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
+         'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][now[1] - 1],
+        now[0], now[3], now[4], now[5],
+        zone)