python - pytz and astimezone() cannot be applied to a naive datetime -


i have date , need make time zone aware.

local_tz = timezone('asia/tokyo') start_date = '2012-09-27' start_date = datetime.strptime(start_date, "%y-%m-%d")    start_date = start_date.astimezone(local_tz)   now_utc = datetime.now(timezone('utc')) local_now = now_utc.astimezone(local_tz) 

i need find if true:

print start_date>local_now 

but error.

   start_date = start_date.astimezone(local_tz)    valueerror: astimezone() cannot applied naive datetime 

i convert utc tokyo no issue. need make start_date timezone aware ad in tokyo.

thanks

for pytz timezones, use .localize() method turn naive datetime object 1 timezone:

start_date = local_tz.localize(start_date) 

for timezones without dst transition, .replace() method attach timezone naive datetime object should work:

start_date = start_date.replace(tzinfo=local_tz) 

see localized times , date arithmetic of pytz documentation more details.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -