【问题标题】:GPS position to TimezoneGPS 位置到时区
【发布时间】:2011-11-30 14:15:01
【问题描述】:

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

var localTime = getLocalTime( lat, long );

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

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

【问题讨论】:

标签: gps timezone gps-time


【解决方案1】:

Google Time Zone API 似乎是您所追求的。但是它没有任何free tier

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

【讨论】:

    【解决方案2】:

    用于计算时区的 shapefile 不再是 not maintained

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

    https://github.com/cstich/gpstotz

    编辑:

    正如 cmets 中提到的,我还应该发布代码。该代码基于 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'
    

    【讨论】:

    • 您应该在此处发布相关代码,而不是要求人们离开现场。指向其他网站的链接可能会过期,并且无法通过 SO 搜索功能进行搜索。
    • 说的有道理,刚刚贴出代码。有没有办法将文件附加到答案?
    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        【解决方案5】:

        你不能简单地使用用户 IP 来确定他们住在哪个地方吗?然后使用 (Countries | Difference with GMT) 的数组来获取当地时间。

        【讨论】:

          猜你喜欢
          • 2015-12-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-28
          • 1970-01-01
          • 1970-01-01
          • 2018-06-12
          相关资源
          最近更新 更多