getting date into rfc822 format

Alex Martelli aleax at aleax.it
Sun Jul 21 05:03:28 EDT 2002


Jon J. Morin wrote:

> Hi all.  I'm writing a program that sends email in an environment that
        ...
> date = time.ctime(time.time())
> 
> Which gives me a date like this:
> Sat Jul 20 00:02:03 2002
> 
> When what I want is something like this:
> Fri, 01 Mar 2002 00:18:17 -0500 (EST)

Well, you're almost 5 months late for the latter.  However:

>>> import email
>>> email.Utils.formatdate()
'Sun, 21 Jul 2002 09:00:38 -0000'
>>> import time
>>> email.Utils.formatdate(time.time()-3600*24)
'Sat, 20 Jul 2002 09:01:01 -0000'
>>>

and if you insist on using your local timezone rather than UTC:

>>> email.Utils.formatdate(localtime=True)
'Sun, 21 Jul 2002 11:01:58 +0200'
>>>


That's what the +0200 &c is by RFC 2822, btw -- offset of your
local timezone from UTC, positive in most of Europe, Asia, and
Africa, negative in the Americas.


Alex




More information about the Python-list mailing list