Discuss / Python / 记录一下

记录一下

Topic source

def to_timestamp(dt_str, tz_str):

    #将输入时间转变为datetime

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

    #设置正则表达式,提取时区

    s=r'^\w{3}(.?\d+).*'

    c_utc=re.match(s,tz_str).group(1)

    #把输入的时间绑定指定的时区

    tz_utc = timezone(timedelta(hours=int(c_utc)))

    ctime=ctime.replace(tzinfo=tz_utc)

    #返回timestamp

    return ctime.timestamp()


  • 1

Reply