Getting Time Zone from Lat Long Coordinates? [duplicate] Getting Time Zone from Lat Long Coordinates? [duplicate] python python

Getting Time Zone from Lat Long Coordinates? [duplicate]


With tzwhere and pytz:

import datetimeimport pytzfrom tzwhere import tzwheretzwhere = tzwhere.tzwhere()timezone_str = tzwhere.tzNameAt(37.3880961, -5.9823299) # Seville coordinatestimezone_str#> Europe/Madridtimezone = pytz.timezone(timezone_str)dt = datetime.datetime.now()timezone.utcoffset(dt)#> datetime.timedelta(0, 7200)


I was able to do a lookup suitable for my purposes using timezonefinder:

import datetimeimport timezonefinder, pytztf = timezonefinder.TimezoneFinder()# From the lat/long, get the tz-database-style time zone name (e.g. 'America/Vancouver') or Nonetimezone_str = tf.certain_timezone_at(lat=49.2827, lng=-123.1207)if timezone_str is None:    print "Could not determine the time zone"else:    # Display the current time in that time zone    timezone = pytz.timezone(timezone_str)    dt = datetime.datetime.utcnow()    print "The time in %s is %s" % (timezone_str, dt + timezone.utcoffset(dt))

There's a discussion of the methods of timezonefinder and its limitations in the documentation linked from its pypi page.

timezonefinder and pytz can be found in the pip packages of the same name.


This works as expected:

import geonamesgeonames_client = geonames.GeonamesClient('demo')geonames_result = geonames_client.find_timezone({'lat': 48.871236, 'lng': 2.77928})print geonames_result['timezoneId']

Output:

'Europe/Paris'