Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: replace() takes no keyword arguments on changing timezone

I'm trying to change the timezone of UTC to America/Sao Paulo, but I'm getting this error:

TypeError: replace() takes no keyword arguments

This is my code:

import pytz

local_tz = pytz.timezone('America/Sao_Paulo')
local_dt = candles[0]['time'].replace(tzinfo=pytz.utc).astimezone(local_tz)

Candle time is:

>>> candles[0]['time']
u'2017-08-03T00:03:00.000000Z'

How can I fix this?

like image 484
Filipe Ferminiano Avatar asked Aug 03 '17 03:08

Filipe Ferminiano


1 Answers

You need to convert your candles[0]['time'] which is a unicode string to datetime object.

Here is an example:

import datetime, pytz

a = u'2017-08-03T00:03:00.000000Z'
local_tz = pytz.timezone('America/Sao_Paulo')
local_dt = datetime.datetime.strptime(a, '%Y-%m-%dT%H:%M:%S.000000Z').replace(tzinfo=pytz.utc).astimezone(local_tz)
print local_dt

Output:

2017-08-02 21:03:00-03:00
like image 193
Chiheb Nexus Avatar answered Nov 15 '22 00:11

Chiheb Nexus