寻找位置时区的红宝石 [英] Ruby gem for finding timezone of location

查看:108
本文介绍了寻找位置时区的红宝石的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位置(城市,州),日期和时间,我想将其转换为utc,但需要先查找位置的时区。我已经做了一些研究,一切似乎都指向earthtools或geonames,但这两个webservices似乎只是经纬度。是否有服务或创业板或任何其他方式来找到基于这种格式的位置的时区?或者如何将位置转换为经度和纬度?

I have a location (city, state), date, and time and I want to convert it to utc, but need to first find the timezone of the location. I've done a little research and everything seems to point to either earthtools or geonames but both webservices appear to be latitude and longitude only. Is there a service or gem or any other way to find the timezone based on this format of location? or how can the location be converted to latitude and longitude?

推荐答案

您可以使用谷歌地图地理编码API 。 API的Ruby实现像 GeoKit 。一旦你有了,你可以使用时区宝石轻松获取经纬度的时区。该宝石可以很容易地在您的时区进行时间转换。

You can easily get a latitude and longitude using the google maps geocoding API. There are ruby implementations for the API like GeoKit. Once you have that you can use the timezone gem to easily get the timezone of a latitude and longitude. The gem makes it easy to do time conversions in your timezone as well.

以下是一个使用 GeoKit TimeZone

require 'geokit'
require 'timezone'

res = Geokit::Geocoders::GoogleGeocoder.geocode('140 Market St, San Francisco, CA')
timezone = Timezone::Zone.new(:latlon => res.ll)

timezone.zone
=> "America/Los_Angeles"
timezone.time Time.now
=> 2011-12-01 14:02:13 UTC

这篇关于寻找位置时区的红宝石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆