import re
from datetime import datetime, timezone, timedelta
def to_timestamp(dt_str, tz_str):
rec = re.compile(r'^(UTC)([\+\-])(0[0-9]|1[0-9]|2[0-3]|[0-9])\:00$')
dt = datetime.strptime(dt_str,'%Y-%m-%d %H:%M:%S')
tz_1 = rec.match(tz_str).group(2)
tz_2 = rec.match(tz_str).group(3)
zone = int(tz_1 + tz_2)
tz_utc = timezone(timedelta(hours = zone))
dt_time = dt.replace(tzinfo = tz_utc)
return dt_time.timestamp()
# 测试:
t1 = to_timestamp('2015-6-1 08:10:30', 'UTC+7:00')
print(t1)
assert t1 == 1433121030.0, t1
t2 = to_timestamp('2015-5-31 16:10:30', 'UTC-09:00')
print(t2)
assert t2 == 1433121030.0, t2
print('ok')
Sign in to make a reply
罗什么O
import re
from datetime import datetime, timezone, timedelta
def to_timestamp(dt_str, tz_str):
rec = re.compile(r'^(UTC)([\+\-])(0[0-9]|1[0-9]|2[0-3]|[0-9])\:00$')
dt = datetime.strptime(dt_str,'%Y-%m-%d %H:%M:%S')
tz_1 = rec.match(tz_str).group(2)
tz_2 = rec.match(tz_str).group(3)
zone = int(tz_1 + tz_2)
tz_utc = timezone(timedelta(hours = zone))
dt_time = dt.replace(tzinfo = tz_utc)
return dt_time.timestamp()
# 测试:
t1 = to_timestamp('2015-6-1 08:10:30', 'UTC+7:00')
print(t1)
assert t1 == 1433121030.0, t1
t2 = to_timestamp('2015-5-31 16:10:30', 'UTC-09:00')
print(t2)
assert t2 == 1433121030.0, t2
print('ok')