Search code examples
pythonjsondatetimetimezoneweather-api

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


I am using weatherbit api to get historical weather report. It responds based on UTC or GMT time. But my timezone is different.

For example

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()

It returns weather report GMT/UTC 2018-08-12:00 to 2018-08-12:23 with timestamp.

If i convert the utctimestamp to datetime with timezone('America/Los_Angeles') It starts previous day time (i.e) Los_Angels time is GMT-7:00 Hours.

So I am getting report from 2018-08-11:17 since GMT-7 is applied to given timestamp. But i need weather report for full day.

How can i get weather report from 2018-08-12:00 to 2018-08-12:23 even the timezone changes.


Solution

  • I achieved through set date intervals start_date=2018-08-12:07&end_date=2018-08-13:07 to get 2018-08-12 date full day weather report.

    The history call can hit 24 hours so we can set time based on your timezone.

    Then you just convert the timestamp GMT/UTC to your timezone.

    Day Light time changes also important because sometimes it will increased or decreased.

    So find the UTC offset time and use dynamically.

    For Example:

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

    The given time 2018-08-12:07 is utcoffset time. Now I can get full day report correctly.