import re
from datetime import datetime,timezone,timedelta
def to_timestamp(dt_str,tz_str):
re_tz=re.compile(r'UTC([+|-])0?([0-9])|(1[0-9])|(2[0-4]):00')
if re_tz.match(tz_str).group(1)=='-':
m=-int(re_tz.match(tz_str).group(2))
else:
m=int(re_tz.match(tz_str).group(2))
tz_utc_m=timezone(timedelta(hours=m))
dt=datetime.strptime(dt_str,'%Y-%m-%d %H:%M:%S').replace(tzinfo=tz_utc_m)
return dt.timestamp()
正则表达式的时区更正:
re_tz=re.compile(r'UTC([+|-])0?([0-9])|(1[0-2]):00')
Sign in to make a reply
拖延症↘患者
import re
from datetime import datetime,timezone,timedelta
def to_timestamp(dt_str,tz_str):
re_tz=re.compile(r'UTC([+|-])0?([0-9])|(1[0-9])|(2[0-4]):00')
if re_tz.match(tz_str).group(1)=='-':
m=-int(re_tz.match(tz_str).group(2))
else:
m=int(re_tz.match(tz_str).group(2))
tz_utc_m=timezone(timedelta(hours=m))
dt=datetime.strptime(dt_str,'%Y-%m-%d %H:%M:%S').replace(tzinfo=tz_utc_m)
return dt.timestamp()