Discuss / Python / 打卡

打卡

Topic source

right-chen

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

初版,代码还有待优化和简约

def to_timestamp(dt_str, tz_str):


    m = re.match("^(\d*)-(\d*)-(\d*)\s(\d*):(\d*):(\d*)",dt_str)
    if m==None:
       return None
    dt = datetime(int(m[1]),int(m[2]), int(m[3]), int(m[4]), int(m[5]),int(m[6])) # 用指定日期时间创建datetime
    z = re.match("^UTC(.*?):",tz_str)
    h = 0 if z==None else int(z[1])
    return dt.replace(tzinfo=timezone(timedelta(hours=h))).timestamp()

  • 1

Reply