0

Is there a way to retrieve timezone information based on Country and State/Province in Python?

E.g. The United States and New York will get EST (Eastern Standard Time).

If that's not possible or efficient, is there a way to get timezone based on Country and City instead?

I'm using Python Django in my project. Thanks in advance.

Tahreem Iqbal
  • 985
  • 6
  • 17
  • 43

2 Answers2

1

This should help you with what you are trying to do:

# importing module

    from geopy.geocoders import Nominatim
    from timezonefinder import TimezoneFinder

  
# initialize Nominatim API
geolocator = Nominatim(user_agent="geoapiExercises")
  
# input as a geek
lad = "Dhaka"
print("Location address:", lad)
  
# getting Latitude and Longitude
location = geolocator.geocode(lad)
  
print("Latitude and Longitude of the said address:")
print((location.latitude, location.longitude))
  
# pass the Latitude and Longitude
# into a timezone_at
# and it return timezone
obj = TimezoneFinder()
  
# returns 'Europe/Berlin'
result = obj.timezone_at(lng=location.longitude, lat=location.latitude)
print("Time Zone : ", result)

I got it from here: https://www.geeksforgeeks.org/get-time-zone-of-a-given-location-using-python/

jocsfoy01
  • 11
  • 4
-1

You can use pytz library and use the code below:

for example, to get the US Central Time you must pass the pytz.timezone('US/Central') to the datetime:

from datetime import datetime
import pytz

# For Example US/Central timezone datetime
aware_us_central = datetime.now(pytz.timezone('US/Central'))
print('US Central DateTime', aware_us_central)
Amir Shamsi
  • 319
  • 3
  • 13