Discuss / Python / 修正

修正

Topic source

4Neutrino

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

from datetime import datetime, timezone, timedelta import re

def to_timestamp(dt_str, tz_str): dt = datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S')

mt = re.match(r'UTC([+-]\d+):(\d+)', tz_str)
h = mt.group(1)
m = mt.group(2)
tz = timezone(timedelta(hours = int(h), minutes = int(m)))

d2 = dt.replace(tzinfo = tz)
ts = d2.timestamp()

return ts

  • 1

Reply