擅长:python、mysql、java
<p>如错误消息所述,您需要一个<code>tzinfo</code>子类(即<a href="http://docs.python.org/2.7/library/datetime.html?highlight=tzinfo#tzinfo-objects" rel="nofollow noreferrer">tzinfo object</a>),该子类<code>pytz.timezone</code>从时区字符串返回,但它不理解您提供的偏移格式。在</p>
<p><a href="https://stackoverflow.com/questions/3692869/user-specified-date-time">Another relevant thread to your problem</a>,它链接到这个<a href="http://timezones.appspot.com/" rel="nofollow noreferrer">google app engine application</a>,它还提供一些源代码。如果你愿意的话,这里有一个简单明了的例子。在</p>
<pre><code>class NaiveTZInfo(datetime.tzinfo):
def __init__(self, hours):
self.hours = hours
def utcoffset(self, dt):
return datetime.timedelta(hours=self.hours)
def dst(self, dt):
return datetime.timedelta(0)
def tzname(self, dt):
return '+%02d' % self.hours
</code></pre>
<p>要处理偏移格式,必须为所提供的格式编写自己的解析逻辑。在</p>
^{pr2}$