简体   繁体   English

使用python将UTC时间转换为提供的经度和纬度的本地时间

[英]using python convert UTC time to local time of Longitude and latitude provided

I am using sunrise-sunset api to get sunrise and sunset for day. 我正在使用日出日落api获取一天的日出和日落。

>>> url = "https://api.sunrise-sunset.org/json?lat=36.7201600&lng=-4.4203400&date=2017-07-31"
>>> import urllib2
>>> import json
>>> resp = urllib2.urlopen(url)
>>> data = json.load(resp)
>>> sunriseUtc = data.get("results").get("sunrise")
>>> sunriseUtc
u'5:23:18 AM'

I want to convert this UTC time to local time of that of Long, Lat passed in the URL. 我想将此UTC时间转换为URL中传递的Long,Lat的本地时间。 ie not user local. 即不是本地用户。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Rather than using a web query you might be better off using pyephem, it is pip install pyephem able and will give you the sunrise/set times, (and a lot more), for a given location, (lat/long), based on physical values. 与其使用网络查询,不如使用pyephem,可能会更好一些,它可以通过pyephem pip install pyephem ,并根据给定位置(经度/纬度)为您提供日出/设置时间(以及更多)。物理价值。

>>> import ephem
>>> sun = ephem.Sun()
>>> greenwich = ephem.Observer()
>>> greenwich.lat = '51:28:38'
>>> print(greenwich.horizon)
0:00:00.0
>>> greenwich.date = '2007/10/1'
>>> r1 = greenwich.next_rising(sun)
>>> greenwich.pressure = 0
>>> greenwich.horizon = '-0:34'
>>> greenwich.date = '2007/10/1'
>>> r2 = greenwich.next_rising(sun)
>>> print('Visual sunrise: %s' % r1)
Visual sunrise: 2007/10/1 05:59:30
>>> print('Naval Observatory sunrise: %s' % r2)
Naval Observatory sunrise: 2007/10/1 05:59:50

Using Python 3.8: 使用Python 3.8:

To Begin add the following modules below: 要开始,请在下面添加以下模块:

import time 导入时间

from time import localtime 从本地时间导入

Next, call on the localtime() method and insert your time (in this case, variable "sunriseUtc"): 接下来,调用localtime()方法并插入您的时间(在这种情况下,为变量“ sunriseUtc”):

localtime(sunriseUtc). 本地时间(sunriseUtc)。

That's it. 而已。

If you would like to format the time (how it is presented) you will need to import strftime (abbreviated for string format time) from the time module. 如果您想格式化时间(如何显示时间),则需要从时间模块中导入strftime(缩写为字符串格式时间)。 Then set up the format for your time. 然后为您的时间设置格式。 Then pass the same argument from above: eg 然后从上面传递相同的参数:例如

from time import strftime 从时间导入strftime

strftime("%m/%d/%Y %I:%M:%S %p", localtime(sunriseUtc)) strftime(“%m /%d /%Y%I:%M:%S%p”,localtime(sunriseUtc))

'05/12/2019 05:57:05 AM' '05 / 12/2019 05:57:05 AM'

This worked for me when using the requests module to download data from the openweathermap.org's API. 当使用请求模块从openweathermap.org的API下载数据时,这对我有用。

If you need more support, see time module's documentation: https://docs.python.org/2/library/time.html#time.strftime 如果您需要更多支持,请参阅时间模块的文档: https : //docs.python.org/2/library/time.html#time.strftime

Note: My project was built on the example from "Project: Fetching Current Weather Data" found here: https://automatetheboringstuff.com/chapter14/ 注意:我的项目是基于“项目:获取当前天气数据”中的示例构建的: https//automatetheboringstuff.com/chapter14/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM