如何获得时代数据库名称(Tzinfo)的纬度和经度?
我的Web应用程序需要非常粗略地找到用户,以计算日落时间和日出时间。
由于地理位置功能一定要征求用户的许可,因此我正在以足够好的方式来集思广益,以便在该应用程序中获得该位置。
时区偏移还不够,因为
- 我也需要纬度,并且
- 偏移通常在一个或两个小时上是天文学的错误。
幸运的是,浏览器可以在当今的操作系统中配置的TZ数据库名称(例如欧洲/马德里)获得。现在,这只是一个获得有关城市的坐标的问题。
即使有几种服务为您提供了一个坐标时区,但我还没有看到反向,我也找不到带有坐标的库。
我可以通过地理位置服务创建自己的自己,但这有点乏味且容易出错,因此我首先想检查某人是否有更好的想法。
My web app has a need to very roughly locate the user in order to calculate sunset and sunrise times.
Since geolocation features necessarily ask the user for permission, I'm brainstorming other ways to get at the location in a way that is good enough for this application.
The timezone offset isn't enough, as
- I need latitude as well and
- the offset is often astronomically wrong by one or two hours.
Luckily, browsers can get at the tz database name (eg. Europe/Madrid) configured in the OS these days. Now it's just a question of getting the coordinates for the city in question.
Even though there are several services that give you a timezone for coordinates, I haven't seen the reverse and I couldn't find a library with coordinates either.
I could create my own from geolocation services, but this is a bit tedious and error-prone to do, so I'd first like to check if somebody has a better idea.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
大多数IANA时区的坐标位于
Zone.tab
和Zone1970.tab
文件中,这些文件是时区数据库本身的一部分。您可以在非官方的github存储库。也就是说,我认为您的想法不是一个要解决的问题,他们认为这会做到。在世界上,有一个时区正在使用一个宽阔的纵向区域。最好的例子是中国,它具有IANA时区ID
Asia/Shangahi
,并且位于UTC+08:00。它被称为“北京时间”,并且在整个国家都使用(大约5,000公里),但非正式地观察“ Xinjiang Time”的区域(有关此信息,请访问Wikipedia对于日出/日落计算,最好的选择是使用地理位置API获取LAT/LON坐标,计算在UTC中该位置的结果,然后转换为所需的时区。
如果用户拒绝地理位置许可,则可以退缩到使用IP地址。有很多服务可供选择,无论是免费的还是商业。在您喜欢的搜索引擎中搜索“ IP Geolocation API”以找到它们。当然,它们只会像数据一样准确,在某些情况下,例如使用VPN时,您会出现错误的查找。
The coordinates for most IANA time zones are in the
zone.tab
andzone1970.tab
files that are part of the time zone database itself. You can find them in the official tzdata distribution, or view them in the unofficial GitHub repository.That said, I don't think your idea is a going to work out they way you think it will. There are several parts of the world where a single time zone is in use that spans a wide longitudinal area. The best example is China, which has the IANA time zone id
Asia/Shangahi
and is at UTC+08:00. It is called "Beijing Time" and is in use across the entire country (approximately 5,000 kilometers wide), except for areas that unofficially observe "Xinjiang Time" (more on this on Wikipedia here). This can create a sunrise/sunset difference of up to 4 hours between the eastern and western parts of the country, depending on time of year.For sunrise/sunset calculations, your best bet is to use geolocation APIs to get lat/lon coordinates, calculate results for that position in UTC, then convert to the desired time zone.
In the case where the user denies permission for geolocation, you can fallback to using the IP address. There are many services to choose from, both free and commercial. Search for "IP Geolocation API" in your favorite search engine to find them. Of course, they will only be as accurate as the data, and you'll have erroneous lookups in some cases such as when VPNs are in use.