我想知道我的用戶發送他的請求的當地時間。 基本上,會出現這樣的事,作爲這樣的函數GPS位置到時區
var localTime = getLocalTime(lat, long);
我不知道如果在LAT一個簡單的劃分可以工作,因爲大多數國家不具備完美的幾何形狀。
任何幫助將是偉大的。任何語言都被接受。我想避免調用遠程API。
我想知道我的用戶發送他的請求的當地時間。 基本上,會出現這樣的事,作爲這樣的函數GPS位置到時區
var localTime = getLocalTime(lat, long);
我不知道如果在LAT一個簡單的劃分可以工作,因爲大多數國家不具備完美的幾何形狀。
任何幫助將是偉大的。任何語言都被接受。我想避免調用遠程API。
難道你不能簡單地使用用戶IP來確定他們住在哪裏?然後使用(Countries | Difference with GMT)數組獲取當地時間。
我前幾天在尋找同樣的東西,不幸的是我找不到一個API或一個簡單的函數。原因就像你所說的那樣,國家沒有完美的幾何形狀。您必須創建每個時區的區域表示並查看您的重點所在。我認爲這將是一個痛苦,我不知道它是否可以完成。
我發現的唯一一個描述在這裏:Determine timezone from latitude/longitude without using web services like Geonames.org。基本上你需要一個包含時區信息的數據庫,並且你正在試圖看看哪一個數據庫與你的興趣點最接近。
但是,我一直在尋找靜態解決方案(不使用互聯網),所以如果你可以使用互聯網連接,你可以使用:http://www.earthtools.org/webservices.htm它提供了一個web服務給你的經緯度座標的時區。
Google Time Zone API似乎是你所追求的。
時區API提供地球表面位置的時間偏移數據。請求特定緯度/經度對的時區信息將返回該時區的名稱,與UTC的時差,以及夏令時偏移。
我今天剛剛面對同樣的問題,我不確定我的答案在這段時間後的相關程度如何,但我基本上只是寫了一個Python函數來實現你想要的。你可以在這裏找到它。
https://github.com/cstich/gpstotz
編輯:
正如評論我也應該張貼代碼中提到。該代碼基於Eric Muller的時區shapefile,您可以在此獲得 - http://efele.net/maps/tz/world/。
編輯2:
事實證明shape文件有外部和內部環的有點陳舊定義(基本上外環使用右手法則,而內環使用左手定則)。在任何情況下,菲奧娜似乎都會照顧到這一點,我相應地更新了代碼。
from rtree import index # requires libspatialindex-c3.deb
from shapely.geometry import Polygon
from shapely.geometry import Point
import os
import fiona
''' Read the world timezone shapefile '''
tzshpFN = os.path.join(os.path.dirname(__file__),
'resources/world/tz_world.shp')
''' Build the geo-index '''
idx = index.Index()
with fiona.open(tzshpFN) as shapes:
for i, shape in enumerate(shapes):
assert shape['geometry']['type'] == 'Polygon'
exterior = shape['geometry']['coordinates'][0]
interior = shape['geometry']['coordinates'][1:]
record = shape['properties']['TZID']
poly = Polygon(exterior, interior)
idx.insert(i, poly.bounds, obj=(i, record, poly))
def gpsToTimezone(lat, lon):
'''
For a pair of lat, lon coordiantes returns the appropriate timezone info.
If a point is on a timezone boundary, then this point is not within the
timezone as it is on the boundary. Does not deal with maritime points.
For a discussion of those see here:
http://efele.net/maps/tz/world/
@lat: latitude
@lon: longitude
@return: Timezone info string
'''
query = [n.object for n in idx.intersection((lon, lat, lon, lat),
objects=True)]
queryPoint = Point(lon, lat)
result = [q[1] for q in query
if q[2].contains(queryPoint)]
if len(result) > 0:
return result[0]
else:
return None
if __name__ == "__main__":
''' Tests '''
assert gpsToTimezone(0, 0) is None # In the ocean somewhere
assert gpsToTimezone(51.50, 0.12) == 'Europe/London'
您應在此處發佈相關代碼,而不是要求人員離開現場。與其他網站的鏈接可能會過時,並且無法通過SO搜索功能進行搜索。 –
這很有道理,只是發佈了代碼。有沒有辦法將文件附加到答案? – cstich
好吧,它涉及到一個遠程API,但看看這個問題的答案。它可能會給你你想要的:http://stackoverflow.com/questions/41504/timezone-lookup-from-latitude-longitude。 –