用于查找位置时区的 Ruby gem [英] Ruby gem for finding timezone of location

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

问题描述

我有一个位置(城市、州)、日期和时间,我想将其转换为 UTC,但需要先找到该位置的时区.我做了一些研究,一切似乎都指向 Earthtools 或 geonames,但两个网络服务似乎都只有纬度和经度.是否有服务或 gem 或任何其他方式可以根据这种位置格式查找时区?或者如何将位置转换为纬度和经度?

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.一旦你有了它,你就可以使用 timezone gem 轻松获取经纬度的时区.gem 还可以轻松地在您的时区进行时间转换.

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时区.

Here is an example using GeoKit and 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

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

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