Python Current Time in a City and State, or Country
Asked Answered
B

1

8

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?

Bissonnette answered 27/6, 2015 at 1:6 Comment(6)
Do you know what locationlist is ahead of time?Benthos
Yes. I have a csv file including all the location data, but unfortunately the location data is not standard. As you can see in the example, some of them are city and state, and the others are just country names. In case of the country names, they are usually small countries. I checked manually and I am pretty sure in terms of the countries, they do not have multiple time-zones within them.Bissonnette
related: Get Timezone from City in Python/DjangoJedjedd
If you know the list ahead of time; why don't you store the result localtion -> timezone mapping instead?Jedjedd
Sebastian, you are totally right. This sample code does not represent my exact code. I am going to modify it. In reality, I have a huge dataset of people with their location. 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 mapping table to identify the current time for each person and reports the administrator if it is not a good time to send them.Bissonnette
Sebastian, I wanted to up-vote your second comment too, but I mistakenly undid my up-vote and I am not able to up-vote it again. Sorry about that. However, I am going to modify my question to reflect your comment and explain my program in more detail.Bissonnette
A
0

Try something more along the lines of this

import datetime
import pytz

utc = pytz.utc
utc_dt = datetime.datetime(2002, 10, 27, 6, 0, 0, tzinfo=utc)
eastern = pytz.timezone('US/Eastern')
loc_dt = utc_dt.astimezone(eastern)
fmt = '%Y-%m-%d %H:%M:%S %Z%z'
loc_dt.strftime(fmt)
Anyways answered 27/6, 2015 at 1:40 Comment(1)
I really did not understand what you mean in this code. I am trying to find the current time in a city or country.Bissonnette

© 2022 - 2024 — McMap. All rights reserved.