1

I have a GMT datetime:

import datetime, pytz
d = datetime.datetime(2020, 11, 27, 12, 0, 0)

Given a "country+sate" or "country+city" pair (when state is not available), or even better, with an ISO 3166 two-part code or a cityId like this:

#case 1
country, state = 'US', 'CA'
#case 2
country, city = 'RU', 'Nowosibirsk'   # here no information of "State" is given
#case 3
iso = 'FR-ARA'  # France, Region Auvergne-Rhone-Alpes
#case 4
cityId = '1003854'       # Germany, Berlin, dataset here: https://developers.google.com/adwords/api/docs/appendix/geo/geotargets-2020-11-18.csv

how to find the local timezone, and get the local datetime?

I know pytz can be useful, but obviously not all cities are listed in it (and even if it's the case, the spelling can vary, see here for the city I mentioned on case #2).

What are usual solutions for this timezone problem?

Basj
  • 41,386
  • 99
  • 383
  • 673
  • pytz lists IANA time zone names, not cities. And with Python 3.9, you have a standard lib module for that, zoneinfo. By the way, your "GMT time" as you show it is a naive datetime object, thus local time. You need to set tzinfo to UTC first. – FObersteiner Nov 28 '20 at 10:06
  • related: [get-timezone-from-city-in-python-django](https://stackoverflow.com/questions/16505501/get-timezone-from-city-in-python-django) – FObersteiner Nov 30 '20 at 15:55
  • 1
    Country+State is not enough info, as several US states have more than one time zone. Country+City is also not enough info, as many city names are duplicated at the country level (ex: [Springfield](https://en.wikipedia.org/wiki/Springfield)). You will need, at minimum, City, State, *and* Country. Then you can geolocate for lat/lon, then you can get a time zone from that. – Matt Johnson-Pint Nov 30 '20 at 17:05
  • @MattJohnson-Pint I think you can post this as an answer, that's the way to go. PS: are there really US states with 2 different hours at the same time *inside* a single state? Would you have an example? – Basj Nov 30 '20 at 20:20
  • Yes. Many. See the image in the duplicate I just closed this under. (close enough dup, same answer). – Matt Johnson-Pint Nov 30 '20 at 21:47

0 Answers0