[英]using python convert UTC time to local time of Longitude and latitude provided
我正在使用日出日落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'
我想将此UTC时间转换为URL中传递的Long,Lat的本地时间。 即不是本地用户。
任何帮助将不胜感激。
与其使用网络查询,不如使用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
使用Python 3.8:
要开始,请在下面添加以下模块:
导入时间
从本地时间导入
接下来,调用localtime()方法并插入您的时间(在这种情况下,为变量“ sunriseUtc”):
本地时间(sunriseUtc)。
而已。
如果您想格式化时间(如何显示时间),则需要从时间模块中导入strftime(缩写为字符串格式时间)。 然后为您的时间设置格式。 然后从上面传递相同的参数:例如
从时间导入strftime
strftime(“%m /%d /%Y%I:%M:%S%p”,localtime(sunriseUtc))
'05 / 12/2019 05:57:05 AM'
当使用请求模块从openweathermap.org的API下载数据时,这对我有用。
如果您需要更多支持,请参阅时间模块的文档: https : //docs.python.org/2/library/time.html#time.strftime
注意:我的项目是基于“项目:获取当前天气数据”中的示例构建的: https : //automatetheboringstuff.com/chapter14/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.