Discuss / Python / lianxi

lianxi

Topic source

米粽粽

#1 Created at ... [Delete] [Delete and Lock User]
from datetime import datetime, timedelta, timezone
import re

def to_timestamp(dt_str, tz_str):
    m = re.match(r'^UTC([+-]\d+)\:\d{2}$', tz_str)
    h = int(m.group(1))
    lt = datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S')
    ut = lt.replace(tzinfo=timezone(timedelta(hours=h)))
    return ut.timestamp()

  • 1

Reply