I have a huge dataset of people with their location data. One of my modules goes through this dataset only ONCE and generates a table in my database to map location data to timezone. This module uses geocoders, tzwhere, and puts.timezone. After that, every time that the administrator wants to send emails to these people, my Django application uses that timezone mapping table to identify the current time for each person and reports the administrator if it is not a good time to send them emails.
The location data includes city and state, or country names.
For this purpose, I combined the following four libraries:
from datetime import datetime
from geopy import geocoders
from tzwhere import tzwhere
from pytz import timezone
In order to generate the timezone mapping table, my module does a similar thing to the following sample 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)
# Store timeZoneObj in the timezone mapping table.
Then, each time the administrator wants to send an email, the Django application goes through each person's timezone and identifies the current time in his/her region. A sample code is as follows:
# for each person:
# find the location and timezoneObj data for this person in the database.
now_time = datetime.now(timezoneObj)
print now_time
Here is the output:
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 any more efficient way to do this?
locationlist
is ahead of time? – Benthos