import re from datetime import datetime, timezone, timedelta
def to_timestamp(dt_str, tz_str):
#将用户输入的时间转换为datetime get_time = datetime.strptime(dt_str,'%Y-%m-%d %H:%M:%S') # 获得用户输入的时区用正则表达式取出 get_utc = re.match(r'^UTC([+|-]\d{1,2}):00$',tz_str) utc = int(get_utc.group(1)) #创建成用户输入的时区 utc_dt = timezone(timedelta(hours=utc)) #设置用户输入的时区 dt = get_time.replace(tzinfo=utc_dt) return dt.timestamp
t1 = to_timestamp('2015-6-1 08:10:30', 'UTC+7:00') assert t1 == 1433121030.0, t1
t2 = to_timestamp('2015-5-31 16:10:30', 'UTC-09:00') assert t2 == 1433121030.0, t2
print('Pass')
Sign in to make a reply
林林1292
import re from datetime import datetime, timezone, timedelta
def to_timestamp(dt_str, tz_str):
t1 = to_timestamp('2015-6-1 08:10:30', 'UTC+7:00') assert t1 == 1433121030.0, t1
t2 = to_timestamp('2015-5-31 16:10:30', 'UTC-09:00') assert t2 == 1433121030.0, t2
print('Pass')