When working with global data, it’s often necessary to convert timestamps from one timezone to another based on geographical coordinates. This is especially useful when you know the current time in one location (e.g., Chicago, IL) and need to determine the corresponding time in another location based on latitude and longitude (e.g., Times Square, NY). In this article, we will walk through a Python script that accomplishes this task using the timezonefinder and pytz libraries.
Step-by-Step Guide
We start by installing the necessary libraries:
pip install timezonefinder pytz
Then, we can use the following script to convert a timestamp in Chicago time to the local time at given latitude and longitude coordinates.
from timezonefinder import TimezoneFinder
from datetime import datetime
import pytz
# Initialize TimezoneFinder
tf = TimezoneFinder()
# Given latitude and longitude (Time Square NY)
latitude = 40.758896
longitude = -73.985130
# Given timestamp in Chicago time
timestamp_str = '5/22/2024 2:00:00 PM'
timestamp_format = '%m/%d/%Y %I:%M:%S %p'
chicago_tz = pytz.timezone('America/Chicago')
# Parse the timestamp string to a datetime object
timestamp = datetime.strptime(timestamp_str, timestamp_format)
timestamp = chicago_tz.localize(timestamp)
# Function to get the timezone for given latitude and longitude
def get_timezone(latitude, longitude):
return tf.timezone_at(lng=longitude, lat=latitude)
# Get the timezone for the given latitude and longitude
local_timezone_str = get_timezone(latitude, longitude)
local_timezone = pytz.timezone(local_timezone_str)
# Convert the Chicago timestamp to UTC
timestamp_utc = timestamp.astimezone(pytz.utc)
# Convert the UTC timestamp to the local timezone
local_timestamp = timestamp_utc.astimezone(local_timezone)
# Format the local timestamp without the timezone offset
local_timestamp_str = local_timestamp.strftime('%Y-%m-%d %H:%M:%S')
print(f"The local time at {local_timezone_str} (latitude: {latitude}, longitude: {longitude}) is {local_timestamp_str}.")

Explanation
- Initialization:
- We start by importing the required libraries:
timezonefinder,datetime, andpytz. - We initialize
TimezoneFinderto help us determine the timezone based on geographic coordinates.
2. Coordinates and Timestamp:
- The script is set up to work with a specific latitude (40.758896) and longitude (-73.985130), along with a timestamp string representing Chicago time (
'5/22/2024 2:00:00 PM').
3. Parsing and Localizing the Timestamp:
- The timestamp string is parsed into a
datetimeobject. - This
datetimeobject is then localized to the Chicago timezone.
4. Determining the Local Timezone:
- The
get_timezonefunction usestimezonefinderto find the timezone corresponding to the given latitude and longitude. - This timezone string is then used to create a
pytztimezone object.
5. Converting Timezones:
- The Chicago-localized timestamp is converted to UTC.
- The UTC timestamp is then converted to the local timezone corresponding to the given coordinates.
6. Formatting and Output:
- The local timestamp is formatted to exclude the timezone offset.
- Finally, the formatted local time is printed.
Conclusion
This script provides a practical way to convert timestamps from one timezone to another based on geographic coordinates. Such a conversion is essential in many applications, including data analysis, scheduling, and real-time systems. By using timezonefinder and pytz, we can ensure accurate and efficient timezone conversions.
If you found this article helpful, please give it a like 👏️ and leave a comment below! Your feedback and engagement help me create more content that benefits the community. Feel free to share your thoughts and any questions you might have. Happy coding!








Leave a comment