我正在尝试获得纬度和经度坐标的时区,但我遇到了一些问题错误可能是非常基本的
我在数据库中有一个表,大约有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) 我已经验证了dateutils.tz.tzlocal()在heroku上不起作用,即使它确实如此,它不会只是从计算机的操作系统中获取tz,而不是用户吗?
如果没有存储用户时区,有没有办法确定请求的来源?(我正在使用烧瓶)
Twitter确实有一个设置来调整你的时区,但我想知道他们如何确定默认值应该是什么以及当用户没有登录时它将如何工作.
我有一个庞大的人员数据集及其位置数据.我的一个模块仅通过此数据集ONCE,并在我的数据库中生成一个表,以将位置数据映射到时区.该模块使用geocoders,tzwhere和puts.timezone.之后,每次管理员想要向这些人发送电子邮件时,我的Django应用程序使用该时区映射表来识别每个人的当前时间,并报告管理员是否不是发送电子邮件的好时机.
位置数据包括城市和州或国家/地区名称.
为此,我结合了以下四个库:
from datetime import datetime
from geopy import geocoders
from tzwhere import tzwhere
from pytz import timezone
Run Code Online (Sandbox Code Playgroud)
为了生成时区映射表,我的模块与以下示例代码类似:
g = geocoders.GoogleV3()
tz = tzwhere.tzwhere()
locationList = ["Sackville, Canada", "Romania", "Mannheim, Germany", "New Delhi, India", "Trier, Germany", "Basel, Switzerland", "Bruxelles/Brussel, Belgium"]
for location in locationList:
place, (lat, lng) = g.geocode(location)
timeZoneStr = tz.tzNameAt(lat, lng)
timeZoneObj = timezone(timeZoneStr)
# Store timeZoneObj in the timezone mapping table.
Run Code Online (Sandbox Code Playgroud)
然后,每次管理员想要发送电子邮件时,Django应用程序都会遍历每个人的时区并识别他/她所在地区的当前时间.示例代码如下:
# for each person:
# find the location and timezoneObj data for this …Run Code Online (Sandbox Code Playgroud)