Joh*_*ith 15 python timezone latitude-longitude geonames
我正在尝试获得纬度和经度坐标的时区,但我遇到了一些问题错误可能是非常基本的
我在数据库中有一个表,大约有600行.每行包含一个lat长坐标,用于世界某处我想将这些坐标输入函数然后检索时区.目的是将这600个地点中每个地点都有本地时间戳的事件转换为UTC时间
当我尝试运行代码时,我收到错误geonames is not defined
.我申请了一个地理名称帐户.
我想我刚刚将函数文件保存在错误的目录或简单的东西中.谁能帮忙
#-------------------------------------------------------------------------------
# Converts latitude longitude into a time zone
# REF: https://gist.github.com/pamelafox/2288222
# REF: http://blog.pamelafox.org/2012/04/converting-addresses-to-timezones-in.html
#-------------------------------------------------------------------------------
geonames_client = geonames.GeonamesClient('Username_alpha')
geonames_result = geonames_client.find_timezone({'lat': 48.871236, 'lng': 2.77928})
user.timezone = geonames_result['timezoneId']
Run Code Online (Sandbox Code Playgroud)
tux*_*ayo 21
使用tzwhere和pytz:
import datetime
import pytz
from tzwhere import tzwhere
tzwhere = tzwhere.tzwhere()
timezone_str = tzwhere.tzNameAt(37.3880961, -5.9823299) # Seville coordinates
timezone_str
#> Europe/Madrid
timezone = pytz.timezone(timezone_str)
dt = datetime.datetime.now()
timezone.utcoffset(dt)
#> datetime.timedelta(0, 7200)
Run Code Online (Sandbox Code Playgroud)
这按预期工作:
import geonames
geonames_client = geonames.GeonamesClient('demo')
geonames_result = geonames_client.find_timezone({'lat': 48.871236, 'lng': 2.77928})
print geonames_result['timezoneId']
Run Code Online (Sandbox Code Playgroud)
输出:
'Europe/Paris'
Run Code Online (Sandbox Code Playgroud)
我可以使用timezonefinder进行适合自己目的的查找:
import datetime
import timezonefinder, pytz
tf = timezonefinder.TimezoneFinder()
# Get the tz-database-style time zone name (e.g. 'America/Vancouver') or None
timezone_str = tf.certain_timezone_at(lat=49.2827, lng=-123.1207)
if timezone_str is None:
print "Could not determine the time zone"
else:
# Display the current time in that time zone
timezone = pytz.timezone(timezone_str)
dt = datetime.datetime.utcnow()
print "The time in %s is %s" % (timezone_str, dt + timezone.utcoffset(dt))
Run Code Online (Sandbox Code Playgroud)
在pypi页面上讨论了timezonefinder的方法及其局限性。
timezonefinder
并且pytz
可以在发现pip
同名的包。
归档时间: |
|
查看次数: |
16988 次 |
最近记录: |