How to get time zone by passing city and country?
To resolve a time zone by city and country, you will first need to obtain latitude/longitude coordinates. There are multiple ways to do that, including various databases of cities, airport codes, or by reverse geocoding addresses.
Be aware that there are some cities in the world that have portions that observe different time zones. In those cases, you may have some small margin of error. See this post: can 2 timezone be for 1 city?
Since you said you are looking for locations of specific places (hotels), you would probably be better off geocoding the exact coordinates of each place using its full address. There are many services for this, such as the one provided by Google here.
Then use the coordinates in one of the services described here: How to get a time zone from a location using latitude and longitude coordinates?
Once you have the time zone id, such as Asia/Seoul
or America/New_York
, then you can use it in pytz functions, as user falsetru showed in his answer.
You could speed things up a bit by checking ahead of time if the city you are interested is already one of the ones defined in the time zone database. But there are thousands of more cities in the world than just those key cities with time zone ids.
Other Ideas
- You could use GeoDjango with time zone map data found here.
- You could look at Django-GeoNames and see if the time zones from GeoNames are included.
- There might be some other Django or Python specific library out there exactly for this that I didn't find in my 5 minutes searching Google.
Get time zone information of the system in Python?
Check out the Python Time Module.
from time import gmtime, strftime
print(strftime("%z", gmtime()))
Pacific Standard Time
Getting Time Zone from Lat Long Coordinates?
This works as expected:
import geonames
geonames_client = geonames.GeonamesClient('demo')
geonames_result = geonames_client.find_timezone({'lat': 48.871236, 'lng': 2.77928})
print geonames_result['timezoneId']
Output:
'Europe/Paris'
Related Topics
Oserror: [Winerror 193] %1 Is Not a Valid Win32 Application
How to Make an Image with a Transparent Backround in Pygame
How to Access a Dictionary Key Value Present Inside a List
Why Do Many Examples Use 'Fig, Ax = Plt.Subplots()' in Matplotlib/Pyplot/Python
How to Use Python to Get the System Hostname
Setting Django Up to Use MySQL
Python SQLite Parameter Substitution with Wildcards in Like
How to Use a (Random) *.Otf or *.Ttf Font in Matplotlib
Can You Give a Django App a Verbose Name for Use Throughout the Admin
Python Regular Expression Re.Match, Why This Code Does Not Work
List' Object Has No Attribute 'Get_Attribute' While Iterating Through Webelements
Adding Labels in X Y Scatter Plot with Seaborn
File Read Using "Open()" VS "With Open()"
How to Get 'Real-Time' Information Back from a Subprocess.Popen in Python (2.5)
How to Assign a Value to a Tensorflow Variable
What's the Cleanest Way to Extract Urls from a String Using Python