Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Time Zone from Lat Long Coordinates? [duplicate]

Tags:

I am trying to get the time zones for latitude and longitude coordinates but am having a few problems The mistakes are probably very basic

I have a table in a database with around 600 rows. Each row contains a lat long coordinate for somewhere in the world I want to feed these co-ordinates into a function and then retrieve the time zone. The aim being to convert events which have a local time stamp within each of these 600 places into UTC time

I found a blog post which uses a piece of code to derive timezones from geographical coordinates.

When I try to run the code, I get the error geonames is not defined. I have applied for an account with geonames.

I think I have just saved the function file in the wrong directory or something simple. Can anyone help

#------------------------------------------------------------------------------- # Converts latitude longitude into a time zone # REF: https://gist.github.com/pamelafox/2288222 # REF: http://blog.pamelafox.org/2012/04/converting-addresses-to-timezones-in.html #-------------------------------------------------------------------------------  geonames_client = geonames.GeonamesClient('Username_alpha') geonames_result = geonames_client.find_timezone({'lat': 48.871236, 'lng': 2.77928}) user.timezone = geonames_result['timezoneId'] 
like image 597
John Smith Avatar asked Apr 01 '13 10:04

John Smith


People also ask

How do you find time zones using latitude and longitude?

Here's how longitude converts into your personal time zone: 15 degrees of longitude = 1 hour difference; 1 degree longitude = 4 minutes difference. 15 minutes of longitude = 1 minute difference; 1 minute of longitude = 4 seconds difference.

How are time zones connected to longitude?

Time zones are another arbitrary societal choice, like the origin point of the Prime Meridian. They are based on longitude and defined by Earth's rotation, which completes a full circle (360 degrees) each day (24 hours). Each hour then, Earth rotates through 360/24 = 15° of longitude: the width of one time zone.


1 Answers

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) 
like image 101
tuxayo Avatar answered Oct 22 '22 03:10

tuxayo