Get Time Zone of a Given Location using Python
In this article, we are going to see how to get the time zone from a given location. The Timezonefinder module is able to find the timezone of any point on earth (coordinates) offline. Before starting we need to install this module.
- timezonefinder: This module does not built in with Python. To install this type the below command in the terminal.
pip install timezonefinder
- Geopy: .geopy makes it easy for Python developers to locate the coordinates of addresses, cities, countries, and landmarks across the world. To install the Geopy module, run the following command in your terminal.
pip install geopy
Let’s understand this module with Step by step:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Step 1: import TimezoneFinder module
Step 2: make an object of TimezoneFinder.
Step 3: Pass the latitude and longitude in a timezone_at() method and it return the time zone of a given location.
Now let write a script to get timezone with a specific location.
- Import the module
- Initialize Nominatim API to get location from the input string.
- Get latitude and longitude with geolocator.geocode() function.
- Pass the latitude and longitude in a timezone_at() method and it returns the time zone of a given location.
Location address: Dhaka Latitude and Longitude of the said address: (23.810651, 90.4126466) Time Zone : Asia/Dhaka