def trans_timezone(time_str_source:str, tz_source, tz_target):
from datetime import datetime
from dateutil import tz
from pytz import timezone
time_source = datetime.strptime(time_str_source, "%Y-%m-%d %H:%M:%S")
time_source_with_tz = datetime(time_source.year, time_source.month, time_source.day,
time_source.hour, time_source.minute, time_source.second, tzinfo=tz.gettz(tz_source))
time_target = time_source_with_tz.astimezone(timezone(tz_target))
# return time_target
return datetime.strftime(time_target, "%Y%m%d-%H:%M:%S")
trans_timezone('2023-08-22 13:53:07', "Hongkong", "US/Central")
def trans_tz(time_str, src_tz, dst_tz):
from datetime import datetime
from pytz import timezone
gmt_time = datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
tz = timezone(dst_tz)
trade_time = timezone(src_tz).localize(gmt_time).astimezone(tz).replace(tzinfo = None)
return datetime.strftime(trade_time, "%Y%m%d-%H:%M:%S")
trans_tz('2023-08-22 13:53:07',"Hongkong", "US/Central")