Formatting RFC 822 date

Sheila King sheila at spamcop.net
Wed Apr 4 09:26:21 EDT 2001


On 3 Apr 2001 18:25:45 GMT, brey at ductape.net wrote in comp.lang.python in
article <9ad4j9$t59$1 at news.netmar.com>:

:I'm using smtplib to send an email message, and I'd like to put a Date: line
:in the header.  However, formatting the timezone portion of the date line
:correctly seems to be a real highly non-trivial.  time.strftime only outputs
:the time zone as a string, and none of the variables in the time module are
:easy to convert to the -600 style format.  Ironically, there is
:rfc822.parsedate, which does the opposite of what I need (and is probably
:much harder to write).
:
:Is there a module that I'm missing that will take a time tuple and output the
:proper RFC 822 date string?

I'm writing a script right now, where I also need such date formatting. The
rfc822 module doesn't provide what you need. It really isn't for composing
messages, but is (rather) for analyzing an already written message.

I've put the following code into my script:

from time import strftime, gmtime, time

date = strftime("%a, %d %b %Y %H:%M:%S", gmtime(time()))
DateField = "Date: " + date + " UT\n"

Here is an interpreter session:

Python 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.6 -- press F1 for help
>>> from time import strftime, gmtime, time
>>> date = strftime("%a, %d %b %Y %H:%M:%S", gmtime(time()))
>>> DateField = "Date: " + date + " UT\n"
>>> print DateField
Date: Wed, 04 Apr 2001 13:22:31 UT

>>> 

It doesn't do the -0600 offset, but it shouldn't be too hard to add that,
using the time module. I'm just trying to keep the overhead on my script low,
and decided that slapping the UT time zone designation on all date fields was
good enough, instead of figuring out time offset.

For what it's worth, I did read rfc822 and believe that my time field above is
an RFC 822 compliant data stamp. I also looked at the date stamps on a number
of the emails in my mail reader (that other people have sent to me) and was
surprised at the number of variations, and they don't seem to comply exactly
with RFC 822.

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/




More information about the Python-list mailing list