[Python-checkins] CVS: python/dist/src/Lib/email Utils.py,1.3,1.4

Barry Warsaw bwarsaw@users.sourceforge.net
Fri, 09 Nov 2001 08:59:58 -0800


Update of /cvsroot/python/python/dist/src/Lib/email
In directory usw-pr-cvs1:/tmp/cvs-serv29582

Modified Files:
	Utils.py 
Log Message:
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.


Index: Utils.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/email/Utils.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** Utils.py	2001/10/04 17:05:11	1.3
--- Utils.py	2001/11/09 16:59:56	1.4
***************
*** 10,14 ****
  from rfc822 import dump_address_pair
  from rfc822 import AddrlistClass as _AddrlistClass
! from rfc822 import parsedate_tz, parsedate, mktime_tz, formatdate
  
  from quopri import decodestring as _qdecode
--- 10,14 ----
  from rfc822 import dump_address_pair
  from rfc822 import AddrlistClass as _AddrlistClass
! from rfc822 import parsedate_tz, parsedate, mktime_tz
  
  from quopri import decodestring as _qdecode
***************
*** 103,104 ****
--- 103,140 ----
          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)