Python – What time is it in Turkey?

What time is it in Turkey?… here is a solution to the problem.

What time is it in Turkey?

I ran at noon in Turkey: Here’s what I got :

2017-12-22 20:11:46.038218+03:00

import pytz
from pytz import timezone
from datetime import datetime

utc_now = datetime.now()
utc = pytz.timezone('UTC')
aware_date = utc.localize(utc_now)
turkey = timezone('Europe/Istanbul')
now_turkey = aware_date.astimezone(turkey)

Why am I getting 20:11:46?

Solution

Since the base time is not correct, change utc_now = datetime.now() to utc_now = datetime.utcnow().

As @RemcoGerlich says, you should use utcnow to get UTC.

Full code:

import pytz
from pytz import timezone
from datetime import datetime

utc_now = datetime.utcnow()
utc = pytz.timezone('UTC')
aware_date = utc.localize(utc_now)
turkey = timezone('Europe/Istanbul')
now_turkey = aware_date.astimezone(turkey)

Related Problems and Solutions