diff options
author | Barry Warsaw <barry@python.org> | 2001-11-09 16:59:56 (GMT) |
---|---|---|
committer | Barry Warsaw <barry@python.org> | 2001-11-09 16:59:56 (GMT) |
commit | aa79f4d492abd5b979e740520e95fcb717793061 (patch) | |
tree | 7ef8f810c4b7428fa05d45a061ca9e645a12e661 /Lib/email/Utils.py | |
parent | 3e77423a641e1331f8e0724328c3385a6a455ce1 (diff) | |
download | cpython-aa79f4d492abd5b979e740520e95fcb717793061.zip cpython-aa79f4d492abd5b979e740520e95fcb717793061.tar.gz cpython-aa79f4d492abd5b979e740520e95fcb717793061.tar.bz2 |
formatdate(): An implementation to replace the one borrowed from
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.
Diffstat (limited to 'Lib/email/Utils.py')
-rw-r--r-- | Lib/email/Utils.py | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/Lib/email/Utils.py b/Lib/email/Utils.py index 6bbf2d3..237b686 100644 --- a/Lib/email/Utils.py +++ b/Lib/email/Utils.py @@ -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) + + + +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) |