How to get Timezone from latitude/longitude ?

Chris Angelico rosuav at gmail.com
Wed Jun 25 09:27:24 EDT 2014


On Wed, Jun 25, 2014 at 10:53 PM,  <codetarsier at gmail.com> wrote:
> I'm looking for a python-library which can help me to get Timezone and Timezone-offset(UTC) from latitude/longitude.
>
> I'm not able to find an easy way to do it.

There isn't an easy way to do it.

At best, you would have to do a two-step lookup: first, figure out
what country and city you're in, based on lat/long; and then, look up
the appropriate timezone from the city. Unfortunately that won't
actually be perfect, because there are points on this planet's surface
that cannot be unambiguously assigned timezones... because timezones
are really messy.

Normally, the way to define timezones would be with the Olsen database
(tzdata), which defines "Australia/Melbourne" (where I live),
"America/Merida" (which is not a reimagining of "Brave" on a different
continent), "Africa/Libreville" (which presumably is where a popular
office suite came from), and so on. If you can get your users to pick
based on those, your work will be easy and unambiguous. Otherwise,
you're going to have a lot of work trying to recreate that...

Why do you need to work this out? Are you trying to use a user's IP
geolocation data to pick a timezone? Because that's doomed to failure.
Even if you have something like a phone's GPS fix, trying to guess the
timezone based on that is a bad idea. In fact, guessing is almost
always a bad idea.

On the other hand, if all you need is a rough approximation, just take
the longitude (positive if you're east of Greenwich, negative if
west), and multiply it by 24 hours/360° to get a theoretical timezone.
That's good enough for planting on a map, or for getting a rough idea
of whether it's morning, noon, evening, or night; most actual civil
timezones will be broadly near to that figure. (Some are a long way
away from it, though. New Zealand's Chatham Islands are at around
176.5°W, for a theoretical timezone of roughly UTC-11:45, but the
actual timezone is UTC+12:45, almost but not exactly an entire day
ahead of where you might think.) Of course, that ignores every civil
invention, including the abomination of DST, but that might be
considered an advantage rather than a flaw...

ChrisA



More information about the Python-list mailing list