如何使用 weatherbit.io api 甚至时区更改获取特定日期的天气预报?

How to get weather report for a particular date using weatherbit.io api even timezone changes?

我正在使用 weatherbit api 获取历史天气报告。它根据 UTC 或 GMT 时间响应。但是我的时区不同。

例如

api_values_call = requests.get('https://api.weatherbit.io/v2.0/history/hourly?key=yourapikey&city=seattle,WA&start_date=2018-08-12:00&end_date=2018-08-12:24&units=I')
api_values = api_values_call.json()

它returns天气预报GMT/UTC 2018-08-12:00 至 2018-08-12:23 带时间戳。

如果我将 utctimestamp 转换为 datetime with timezone('America/Los_Angeles') 它从前一天时间开始(即)Los_Angels 时间是 GMT-7:00 小时。

所以我从 2018-08-11:17 收到报告,因为 GMT-7 应用于给定的时间戳。但我需要全天的天气预报。

即使时区发生变化,我如何获取从 2018-08-12:00 到 2018-08-12:23 的天气预报。

我通过设定的日期间隔 start_date=2018-08-12:07&end_date=2018-08-13:07 获得 2018-08-12日全天天气预报

历史调用可以达到 24 小时,因此我们可以根据您的时区设置时间。

然后您只需将时间戳 GMT/UTC 转换为您的时区。

日光时间变化也很重要,因为有时它会增加或减少。

于是找到UTC偏移时间动态使用

例如:

tz = pytz.timezone('America/Los_Angeles')
dt = datetime.strptime(given_date, '%Y-%m-%d')
offset = int(tz.utcoffset(dt).total_seconds() / 3600.0)

给定时间2018-08-12:07为utcoffset时间。现在我可以正确获取全天报告了。