I have a huge dataset with location data. One of my modules goes through this ONCE only dataset and generates a table in my database to display location data in the time zone. This module uses geocoders, tzwhere and puts.timezone. After that, every time the administrator wants to send emails to these people, my Django application uses this time zone mapping table to determine the current time for each person and tells the administrator if they cannot send them by email.
Location data includes city and state, or country names.
To this end, I have combined the following four libraries:
from datetime import datetime from geopy import geocoders from tzwhere import tzwhere from pytz import timezone
To create a timezone mapping table, my module runs the same code for the following code:
g = geocoders.GoogleV3() tz = tzwhere.tzwhere() locationList = ["Sackville, Canada", "Romania", "Mannheim, Germany", "New Delhi, India", "Trier, Germany", "Basel, Switzerland", "Bruxelles/Brussel, Belgium"] for location in locationList: place, (lat, lng) = g.geocode(location) timeZoneStr = tz.tzNameAt(lat, lng) timeZoneObj = timezone(timeZoneStr)
Then, every time the administrator wants to send an email, the Django application goes through the time zone of each person and identifies the current time in his region. Sample code is as follows:
# for each person:
Here is the result:
Sackville, Canada : 2015-06-26 22:00:18.131000-03:00 Romania : 2015-06-27 04:00:18.240000+03:00 Mannheim, Germany : 2015-06-27 03:00:18.371000+02:00 New Delhi, India : 2015-06-27 06:30:18.531000+05:30 Trier, Germany : 2015-06-27 03:00:18.656000+02:00 Basel, Switzerland : 2015-06-27 03:00:18.812000+02:00 Bruxelles/Brussel, Belgium : 2015-06-27 03:00:18.921000+02:00
Is there a more efficient way to do this?