11

我想知道我的用户发送请求的当地时间。基本上,有没有像这样的功能

var localTime = getLocalTime( lat, long );

我不确定在纬度上的简单划分是否可行,因为大多数国家都没有完美的几何形状。

任何帮助都会很棒。接受任何语言。我想避免调用遥远的 API。

4

5 回答 5

4

Google Time Zone API似乎正是您所追求的。但是,它没有任何免费层

时区 API 提供地球表面位置的时间偏移数据。请求特定纬度/经度对的时区信息将返回该时区的名称、与 UTC 的时间偏移以及夏令时偏移。

于 2013-12-10T00:18:34.853 回答
3

不再维护用于计算时区的 shapefile 。

我今天刚遇到同样的问题,我不确定我的答案在这段时间之后有多相关,但我基本上只是写了一个 Python 函数来做你想做的事。你可以在这里找到它。

https://github.com/cstich/gpstotz

编辑:

正如评论中提到的,我还应该发布代码。该代码基于 Eric Muller 的时区 shapefile,您可以在此处获取 - http://efele.net/maps/tz/world/

编辑2:

事实证明,shapefile 对外环和内环的定义有些过时(基本上外环使用右手定则,而内环使用左手定则)。在任何情况下,fiona 似乎都会解决这个问题,我相应地更新了代码。

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'
于 2015-05-25T19:30:49.663 回答
2

几天前我正在寻找同样的东西,不幸的是我找不到一个 API 或一个简单的函数来做到这一点。原因正如您所说,国家没有完美的几何形状。您必须创建每个时区区域的表示并查看您的观点所在。我认为这将是一种痛苦,我不知道是否可以做到。

我发现的唯一一个在这里描述:Determine timezone from latitude/longitude without using web services like Geonames.org。基本上,您需要一个包含有关时区信息的数据库,并且您正在尝试查看哪个最接近您的兴趣点。

但是,我一直在寻找静态解决方案(不使用互联网),所以如果你可以使用互联网连接,你可以使用:http ://www.earthtools.org/webservices.htm它提供了一个网络服务来为你提供给定的时区 lat/朗坐标。

于 2011-11-30T14:26:26.823 回答
1

截至 2019 年,Google API 没有任何免费层,并且不再维护 @cstich answer 的数据源。

如果您需要 API,timezonedb.com提供限制为 1 个请求/秒的免费层级速率。

@cstich 使用的数据的原始维护者链接到该项目,该项目从 OpenStreetMap 检索数据。自述文件包含用于查找各种语言的库的链接。

于 2019-04-30T10:23:46.523 回答
-3

你不能简单地使用用户 IP 来确定他们住在哪个地方吗?然后你使用一组(国家|与格林威治标准时间的差异)来获取当地时间。

于 2011-11-30T14:26:11.837 回答