我使用Rails TZInfo::Timezone
我如何才能在其中添加新的城市。
例如:我想要显示我的应用程序中的'Washington,DC‘时区,但该城市没有可用的映射。
我使用TZInfo::Timezone.get()方法检索所有时区
请给我一些专家的建议。
发布于 2014-12-25 11:15:20
这有点复杂,但是您可以使用地理编码器获取城市的纬度和经度,然后使用它从geonames.org获取时区。这里有一个Ruby的例子:
search_location = 'Washington, DC'
geocoder_results = Geocoder.search(search_location)
# This assumes the city name will always get results from geocoder
nearest_lat = geocoder_results[0].latitude
nearest_lon = geocoder_results[0].longitude
resp = Net::HTTP.get_response(URI.parse('http://api.geonames.org/timezoneJSON?lat=' + nearest_lat.to_s + '&lng=' + nearest_lon.to_s + '&username=your_user_name_here'))
geonames_tz_info = JSON.parse(resp.body)
city_time_zone = geonames_tz_info['timezoneId']
time_zone = TZInfo::Timezone.get(city_time_zone)https://stackoverflow.com/questions/3386604
复制相似问题