Discuss / Python / 将输入的日期和时间和时区信息转换为timestamp。

将输入的日期和时间和时区信息转换为timestamp。

Topic source

灿_Clarence

#1 Created at ... [Delete] [Delete and Lock User]

def to_timestamp(dt_str, tz_str):

    cday = datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S')

    offset=int(re.match(r'^UTC([+|-]\d+):\d{2}$',tz_str).group(1))

    return cday.replace(tzinfo=timezone(timedelta(hours=offset))).timestamp()


  • 1

Reply