Formatting RFC 822 date

Ben Hutchings ben.hutchings at roundpoint.com
Thu Apr 5 20:49:38 EDT 2001


Carey Evans <careye at spamcop.net> writes:

> brey at ductape.net writes:
> 
> > 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.
<snip>
> def zone822(dst):
>     if dst == -1:
>         return '-0000'
>     else:
>         offs = (time.timezone, time.altzone)[dst]
>         return '%+05d' % (offs / -36)
<snip>

The minute digits of the time offset weren't included just for fun;
there really are zones that aren't offset by a whole number of hours.
How about this instead:

    def zone822(dst):
        offs = (0, time.timezone, time.altzone)[1 + dst]
        return '%s%02d%02d' % ("+-"[offs >= 0],
                               abs(offs) / 3600,
                               (abs(offs) / 60) % 60)

-- 
Any opinions expressed are my own and not necessarily those of Roundpoint.



More information about the Python-list mailing list