如何通过城市和国家来获取时区? [英] How to get time zone by passing city and country?

查看:460
本文介绍了如何通过城市和国家来获取时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的django项目中,我有一个小型酒店数据库,其中包含字段 city country



我可以通过传递城市来获得该地点的时区国家作为参数。



我要这样做的原因是因为我有一些基于工作的命令旅馆的入住时间与现在的时间的差额。由于datetime.now()给出的是原始日期时间,因此我无法将其与酒店时间进行比较。



我如何期待解决方案。
谢谢:)

解决方案

要按城市和国家/地区解析时区,您首先需要获取纬度/经度坐标。有多种方法可以做到这一点,包括各种城市数据库,机场代码或通过反向地理编码地址。



请注意,世界上有些城市拥有观察不同时区的部分。在这些情况下,您可能会有少量误差。看到这篇文章:可以为1个城市设置2个时区吗?



由于您说过要查找特定地点(酒店)的位置,因此最好使用其完整地址对每个地点的确切坐标进行地理编码。为此有很多服务,例如 Google在此处提供的服务。 / p>

然后在此处描述的一项服务中使用坐标:如何获取



您具有时区ID,例如 Asia / Seoul America / New_York ,则可以在 pytz 功能,如用户falsetru在其答案中所示。



可以通过提前检查来加快处理速度如果您感兴趣的城市已经是时区数据库中定义的城市之一,则时间间隔为0。但是世界上有成千上万的城市,而不仅仅是那些带有时区ID的主要城市。



其他想法




  • 您可以将 GeoDjango 与时区地图数据一起使用在这里找到

  • 您可以看看< a href = https://github.com/ramusus/django-geonames rel = nofollow noreferrer> Django-GeoNames ,然后查看是否包含GeoNames中的时区。

  • 可能还有其他一些针对Django或Python的库,正是我在搜索Google的5分钟内找不到的库。


In my django project I have a small hotel database which has fields city and country.

Can I get the timezone for that place by passing the city and country as parameters in someway.

The reason I want to do this is because I have some commands that work based on the difference in check-in time of hotels and the time at that hote now . Since datetime.now() gives naive datetime I cannot use it to compare with the hotel time.

How do I look forward to the solution. Thanks :)

解决方案

To resolve a time zone by city and country, you will first need to obtain latitude/longitude coordinates. There are multiple ways to do that, including various databases of cities, airport codes, or by reverse geocoding addresses.

Be aware that there are some cities in the world that have portions that observe different time zones. In those cases, you may have some small margin of error. See this post: can 2 timezone be for 1 city?

Since you said you are looking for locations of specific places (hotels), you would probably be better off geocoding the exact coordinates of each place using its full address. There are many services for this, such as the one provided by Google here.

Then use the coordinates in one of the services described here: How to get a time zone from a location using latitude and longitude coordinates?

Once you have the time zone id, such as Asia/Seoul or America/New_York, then you can use it in pytz functions, as user falsetru showed in his answer.

You could speed things up a bit by checking ahead of time if the city you are interested is already one of the ones defined in the time zone database. But there are thousands of more cities in the world than just those key cities with time zone ids.

Other Ideas

  • You could use GeoDjango with time zone map data found here.
  • You could look at Django-GeoNames and see if the time zones from GeoNames are included.
  • There might be some other Django or Python specific library out there exactly for this that I didn't find in my 5 minutes searching Google.

这篇关于如何通过城市和国家来获取时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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