python datetime.strftime() 无法使用 RqlTzinfo 将日期时间对象字符串化

python datetime.strftime() fails to stringify datetime object with RqlTzinfo

我从 rethinkdb 集群中获取的行包括带时区的日期对象。 这是一个例子:

ipython> dt
datetime.datetime(2015, 12, 18, 0, 22, 4, 644000, tzinfo=<rethinkdb.ast.RqlTzinfo object at 0x7f072c5d6250>)

我正在尝试将它们串成某种格式:

dt.strftime("%d-%m-%Y %H:%M:%S (%Z)")

它导致

*** TypeError: tzinfo.tzname() must return None or a string, not 'unicode'

我该如何克服这个问题?

如果您查看 RqlTzinfo.tzname() 的源代码,您可以看到它只是 returns 它的 offsetstr 属性。由于该属性在 class 中的任何地方都不会被修改或转换,并且 class 的行为不取决于它是 str 还是 unicode,因此以下内容应该足够了:

dt.tzinfo.offsetstr = str(dt.tzinfo.offsetstr)