如何通过earthtools.org从纬度和经度获取TimeZone

时间:2011-10-26 21:34:55

标签: asp.net web-services timezone latitude-longitude

我是编程的先驱。

我有一个ASPX.NET Web应用程序。

我试图通过使用网络服务获得当前loged用户的当前时区。 我在数据库中存储了纬度和经度坐标值。

有人可以给我一个教程链接吗?甚至是代码示例......

1 个答案:

答案 0 :(得分:1)

# offset = TimeZoneFinder.gmt_offset(some_lat, some_lng)
# timezone = ActiveSupport::TimeZone[offset]
#
# timezone = TimeZoneFinder.timezoneId(lat, lng)
# Time.zone = timezone
# Time.zone.now
#
class TimeZoneFinder
  attr_accessor :result

  def self.gmt_offset(lat,lng)
    self.new(lat,lng).find('gmtOffset').to_f
  end

  def self.timezoneId(lat,lng)
    self.new(lat,lng).find('timezoneId')
  end

  def initialize(lat,lng)
    uri     = URI.parse("http://ws.geonames.org/timezone?lat=#{lat}&lng=#{lng}")
    @result = Hash.from_xml(open(uri))
  end

  def find(key)
    result["geonames"]["timezone"][key]
  end
end

# Example XML response...
# <geonames>
#   <timezone>
#     <countryCode>US</countryCode>
#     <countryName>United States</countryName>
#     <lat>41.8781136</lat>
#     <lng>-87.6297982</lng>
#     <timezoneId>America/Chicago</timezoneId>
#     <dstOffset>-5.0</dstOffset>
#     <gmtOffset>-6.0</gmtOffset>
#     <rawOffset>-6.0</rawOffset>
#     <time>2011-09-01 21:40</time>
#     <sunrise>2011-09-02 06:16</sunrise>
#     <sunset>2011-09-02 19:22</sunset>
#   </timezone>
# </geonames>`enter code here`