import pandas as pd
from typing import Callable, Dict,List, Union
from pytz import timezone
TZ_INFO = timezone("Asia/Shanghai") #使用东八区时区
#------------------------------------------------------------------------------------
def utc_to_local(timestamp:Union[str, float,int]):
"""
把timestamp或者str的datetime转化成东八区的datetime
"""
if isinstance(timestamp,str):
if "Z" in timestamp:
if "." in timestamp:
local_time = pd.to_datetime(timestamp,format="%Y-%m-%dT%H:%M:%S.%fZ") + timedelta(hours=8)
else:
local_time = pd.to_datetime(timestamp,format="%Y-%m-%dT%H:%M:%SZ") + timedelta(hours=8)
elif "T" in timestamp:
if "." in timestamp:
local_time = pd.to_datetime(timestamp,format="%Y-%m-%dT%H:%M:%S.%f") + timedelta(hours=8)
else:
local_time = pd.to_datetime(timestamp,format="%Y-%m-%dT%H:%M:%S") + timedelta(hours=8)
elif isinstance(timestamp,float):
local_time = pd.to_datetime(timestamp,unit = "s") + timedelta(hours=8)
elif isinstance(timestamp,int):
#秒时间戳
if len(str(timestamp)) == 10:
local_time = pd.to_datetime(timestamp,unit = "s") + timedelta(hours=8)
#毫秒时间戳
elif len(str(timestamp)) == 13:
local_time = pd.to_datetime(timestamp,unit = "ms") + timedelta(hours=8)
#微妙时间戳
elif len(str(timestamp)) == 16:
local_time = pd.to_datetime(timestamp,unit = "us") + timedelta(hours=8)
#纳秒时间戳
elif len(str(timestamp)) == 19:
local_time = pd.to_datetime(timestamp,unit = "ns") + timedelta(hours=8)
return local_time.tz_localize(TZ_INFO) #pandas timestamp添加时区