1、方法一
def utc_to_local(utc_time_str, utc_format='%Y-%m-%dT%H:%M:%S.%fZ'):
local_tz = pytz.timezone('Asia/Shanghai')
local_format = "%Y-%m-%d %H:%M:%S"
utc_dt = datetime.strptime(utc_time_str, utc_format)
local_dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(local_tz)
time_str = local_dt.strftime(local_format)
return datetime.fromtimestamp(int(time.mktime(time.strptime(time_str, local_format))))
2、方法二
utc = "2018-07-17T08:48:31.151Z"
UTC_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
utcTime = datetime.datetime.strptime(utc, UTC_FORMAT)
localtime = utcTime + datetime.timedelta(hours=8)
print(localtime)
注解:
1、接收到的时间格式为UTC时间:2018-07-17T08:48:31.151Z
2、转换成本地时间戳,并把转换的时间戳通过 datetime.fromtimestamp() 方法转换成本地时间“Asia/Shanghai”