def normalize_date(date):
try:
# See if date is a `time.struct_time`
# if so, convert it and account for leapseconds
tt, date = date, dt(*date[:5] + (min(date[5], 59),))
except TypeError:
pass
else:
is_dst = None if tt[8] is -1 else tt[8]
try:
tm_zone = tt.tm_zone
except AttributeError:
tm_zone = None
tm_gmtoff = None
else:
tm_gmtoff = tt.tm_gmtoff
if tm_zone:
date = pytz.timezone(tm_zone).localize(date, is_dst=is_dst)
elif tm_gmtoff:
offset = tzoffset(None, tm_gmtoff)
date.replace(tzinfo=offset)
# Set timezone to UTC
try:
tzdate = date.astimezone(utc) if date.tzinfo else utc.localize(date)
except AttributeError:
tzdate = date
return tzdate
评论列表
文章目录