在Python / Django中从City获取时区
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在Python / Django中从City获取时区,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2876字,纯文字阅读大概需要5分钟。
内容图文
![在Python / Django中从City获取时区](/upload/InfoBanner/zyjiaocheng/714/3851d49cefd84c2498fe31b8d5651dd0.jpg)
使用pytz,我能够获得如下所示的时区列表:
>>> from pytz import country_timezones
>>> print(' '.join(country_timezones('ch')))
Europe/Zurich
>>> print(' '.join(country_timezones('CH')))
Europe/Zurich
鉴于我从用户那里获得了Country和City字段,我该如何确定城市的时区?
解决方法:
pytz是IANA时区数据库(Olson数据库)的包装器.它不包含将世界上任意城市映射到其所在时区的数据.
您可能需要一个地理编码器,例如geopy,可以使用各种网络服务将地点(例如城市名称)转换为其坐标(纬度,经度):
from geopy import geocoders # pip install geopy
g = geocoders.GoogleV3()
place, (lat, lng) = g.geocode('Singapore')
# -> (u'Singapore', (1.352083, 103.819836))
根据城市的纬度,经度,可以使用tz_world, an efele.net/tz map / a shapefile of the TZ timezones of the world(例如,通过postgis timezone db或pytzwhere)找到其时区:
import tzwhere
w = tzwhere()
print w.tzNameAt(1.352083, 103.819836)
# -> Asia/Singapore
还存在允许将(纬度,经度)转换为时区的web服务,例如askgeo,geonames,参见Timezone lookup from latitude longitude.
截至@dashesy pointed out in the comment,geopy也可以找到时区(从1.2开始):
timezone = g.timezone((lat, lng)) # return pytz timezone object
# -> <DstTzInfo 'Asia/Singapore' LMT+6:55:00 STD>
GeoNames also provides offline data允许直接从其名称获取城市的时区,例如:
#!/usr/bin/env python
import os
from collections import defaultdict
from datetime import datetime
from urllib import urlretrieve
from urlparse import urljoin
from zipfile import ZipFile
import pytz # pip install pytz
geonames_url = 'http://download.geonames.org/export/dump/'
basename = 'cities15000' # all cities with a population > 15000 or capitals
filename = basename + '.zip'
# get file
if not os.path.exists(filename):
urlretrieve(urljoin(geonames_url, filename), filename)
# parse it
city2tz = defaultdict(set)
with ZipFile(filename) as zf, zf.open(basename + '.txt') as file:
for line in file:
fields = line.split(b'\t')
if fields: # geoname table http://download.geonames.org/export/dump/
name, asciiname, alternatenames = fields[1:4]
timezone = fields[-2].decode('utf-8').strip()
if timezone:
for city in [name, asciiname] + alternatenames.split(b','):
city = city.decode('utf-8').strip()
if city:
city2tz[city].add(timezone)
print("Number of available city names (with aliases): %d" % len(city2tz))
#
n = sum((len(timezones) > 1) for city, timezones in city2tz.iteritems())
print("")
print("Find number of ambigious city names\n "
"(that have more than one associated timezone): %d" % n)
#
fmt = '%Y-%m-%d %H:%M:%S %Z%z'
city = "Zurich"
for tzname in city2tz[city]:
now = datetime.now(pytz.timezone(tzname))
print("")
print("%s is in %s timezone" % (city, tzname))
print("Current time in %s is %s" % (city, now.strftime(fmt)))
产量
Number of available city names (with aliases): 112682
Find number of ambigious city names
(that have more than one associated timezone): 2318
Zurich is in Europe/Zurich timezone
Current time in Zurich is 2013-05-13 11:36:33 CEST+0200
内容总结
以上是互联网集市为您收集整理的在Python / Django中从City获取时区全部内容,希望文章能够帮你解决在Python / Django中从City获取时区所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。