这有点复杂,但是您可以使用geocoder获取城市的纬度和经度,然后使用它从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)