How to get the time zone after driving through the city and country?

In my django project, I have a small database of hotels with city and country fields.

Can I get the timezone for this place by passing city and country as parameters in some way.

The reason I want to do this is because I have some teams that work based on the difference in check-in time for hotels and the time for this trick now. Since datetime.now () gives a naive datetime, I cannot use it to compare with the time at the hotel.

How I look forward to a decision. Thanks:)

+4
source share
1 answer

To determine the time zone by city and country, you first need to get the latitude / longitude coordinates. There are several ways to do this, including various city databases, airport codes, or reverse geocoding addresses.

Keep in mind that there are cities in the world in which there are parts in which different time zones are observed. In these cases, you may have a slight margin of error. See this post: 2 time zones can be for 1 city?

Since you said that you were looking for the location of certain places (hotels), you probably would be better off geocoding the exact coordinates of each place using its full address. There are many services available for this, such as those provided by Google here .

Then use the coordinates in one of the services described here: How to get the time zone from a location using latitude and longitude coordinates?

If you have a time zone identifier, for example, Asia/Seoul or America/New_York , then you can use it in pytz functions, as the user falsetru showed in his answer.

You could speed up the process a bit by checking in advance whether the city you are interested in is one of those defined in the time zone database. But there are thousands of cities in the world more than just those key cities with time zone identifiers.

Other ideas

  • You can use GeoDjango with the timezone map data found here .
  • You can look at Django-GeoNames and see if time zones from GeoNames are included.
  • There might be some other Django or Python specific library for this that I could not find in 5 minutes of searching on Google.
+7
source

Source: https://habr.com/ru/post/1493954/