<p>请参阅有关<a href="http://docs.python.org/library/datetime.html?highlight=tzinfo#tzinfo-objects" rel="noreferrer">tzinfo</a>对象的<a href="http://docs.python.org/library/datetime.html?highlight=tzinfo#module-datetime" rel="noreferrer">datetime</a>文档。你必须实现你想要支持自己的时区。这些是文档底部的示例。</p>
<p>下面是一个简单的例子:</p>
<pre><code>from datetime import datetime,tzinfo,timedelta
class Zone(tzinfo):
def __init__(self,offset,isdst,name):
self.offset = offset
self.isdst = isdst
self.name = name
def utcoffset(self, dt):
return timedelta(hours=self.offset) + self.dst(dt)
def dst(self, dt):
return timedelta(hours=1) if self.isdst else timedelta(0)
def tzname(self,dt):
return self.name
GMT = Zone(0,False,'GMT')
EST = Zone(-5,False,'EST')
print datetime.utcnow().strftime('%m/%d/%Y %H:%M:%S %Z')
print datetime.now(GMT).strftime('%m/%d/%Y %H:%M:%S %Z')
print datetime.now(EST).strftime('%m/%d/%Y %H:%M:%S %Z')
t = datetime.strptime('2011-01-21 02:37:21','%Y-%m-%d %H:%M:%S')
t = t.replace(tzinfo=GMT)
print t
print t.astimezone(EST)
</code></pre>
<h3>输出</h3>
<pre><code>01/22/2011 21:52:09
01/22/2011 21:52:09 GMT
01/22/2011 16:52:09 EST
2011-01-21 02:37:21+00:00
2011-01-20 21:37:21-05:00a
</code></pre>