Getting Time Zone from Lat Long Coordinates?
I was able to do a lookup suitable for my purposes using timezonefinder:
import datetime
import timezonefinder, pytz
tf = timezonefinder.TimezoneFinder()
# From the lat/long, get the tz-database-style time zone name (e.g. 'America/Vancouver') or None
timezone_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.
With tzwhere and pytz:
import datetime
import pytz
from tzwhere import tzwhere
tzwhere = tzwhere.tzwhere()
timezone_str = tzwhere.tzNameAt(37.3880961, -5.9823299) # Seville coordinates
timezone_str
#> Europe/Madrid
timezone = pytz.timezone(timezone_str)
dt = datetime.datetime.now()
timezone.utcoffset(dt)
#> datetime.timedelta(0, 7200)