簡體   English   中英

使用python將UTC時間轉換為提供的經度和緯度的本地時間

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM