Discuss / Python / work 2333

work 2333

Topic source

Phioton

#1 Created at ... [Delete] [Delete and Lock User]
# -*- coding:utf-8 -*-

import re
from datetime import datetime, timezone, timedelta

def to_timestamp(dt_str, tz_str, timezone_local=8):
    # 本机时区为 UTC+8:00
    the_time = datetime.strptime(str(dt_str), '%Y-%m-%d %H:%M:%S')
    #解析输入的时间,时区是UTC+8
    time_delta_hour=int(
        re.match(r'^UTC((?:\+|\-)\d+):\d+',str(tz_str)).group(1)
        )-timezone_local
        # 正则表达式抽出 输入的时区,然后-8,得到与本地时区的差
    the_time_local = the_time - timedelta(hours=time_delta_hour)
    # 得到输入时间在本地时区的表示
    return the_time_local.timestamp()
    #返回timestamp表示

  • 1

Reply