擅长:python、mysql、java
<p>以下是一些工作代码,为了清楚起见,它们被分解成简单的片段:</p>
<pre><code>>>> import datetime
>>> import time
>>> from email import utils
>>> nowdt = datetime.datetime.now()
>>> nowtuple = nowdt.timetuple()
>>> nowtimestamp = time.mktime(nowtuple)
>>> utils.formatdate(nowtimestamp)
'Tue, 10 Aug 2010 20:43:53 -0000'
</code></pre>
<p>解释:<code>email.utils.formatdate</code>需要一个时间戳——即一个自纪元以来带有秒(及其小数)的浮点。一个<code>datetime</code>实例不会直接给你一个时间戳——但是,它可以用<code>timetuple</code>方法给你一个时间元组,当然<code>time.mktime</code>也可以用这样的元组来生成时间戳。</p>