Convert Timezone
The timestamp returned in OpenWeatherMap API is the time of the user's local system time.
Here are the steps to convert it to the query location's time:
Convert user's local timestamp to UTC timestamp
import time '''convert user's local timestamp to UTC timestamp''' def get_location_timestamp(): timestamp = data['dt'] offset = time.altzone #Get the offset of user's local DST timezone: utc_timestamp = timestamp + offset return utc_timestamp
Get the location's latitude, longtitude from OpenWeatherMap API
location = '{:f}, {:f}'.format(coord['lat'], coord['lon'])
Access Google Timezone API, pass the location's latitude, longtitude, and the UTC timestamp, get the offset of query location's DST timezone
https://maps.googleapis.com/maps/api/timezone/json?location={latitude, longtitude}×tamp={timestamp}&&language=zh-CN&key={key}
{ "dstOffset" : 0, "rawOffset" : 28800, "status" : "OK", "timeZoneId" : "Asia/Shanghai", "timeZoneName" : "中国标准时间" }
- Calculate the location's timestamp
location_timestamp = utc_timestamp + dstOffset + rawOffset
Get Weekday
from datetime import datetime
import calendar
dt = datetime.fromtimestamp(timestamp)
weekday = calendar.day_name[dt.weekday()]
weekday_abbr = calendar.day_abbr[dt.weekday()]