Search code examples
rethinkdbpython-datetimerethinkdb-python

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


My fetched rows from rethinkdb cluster include date objects with timezones. Here's an example of one:

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

I'm trying to stringify them into a certain format:

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

and it leads to

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

How can I overcome this?


Solution

  • If you look at the source code for RqlTzinfo.tzname(), you can see that it simply returns its offsetstr attribute. Since that attribute is never modified or converted anywhere in that class and the class's behavior does not depend on it being either a str or unicode, the following should suffice:

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