Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - What time is it in Turkey?

I ran when it was noon in turkey..this is 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 did I get 20:11:46?

like image 514
Tampa Avatar asked Sep 14 '25 23:09

Tampa


1 Answers

Because the base time is wrong, just change utc_now = datetime.now() to utc_now = datetime.utcnow() and then it works.

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

Whole 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)
like image 149
Sraw Avatar answered Sep 16 '25 14:09

Sraw