import re from datetime import datetime, timezone, timedelta
def to_timestamp(dt_str, tz_str):
dt = datetime.strptime(dt_str,'%Y-%m-%d %H:%M:%S') sq= re.match(r'UTC([+|-]\d+):00',tz_str).group(1) UTC_x = timezone(timedelta(hours=int(sq))) dt = dt.replace(tzinfo=UTC_x) dt = dt.astimezone(timezone(timedelta(hours=0))) 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
shengscript
-- coding:utf-8 --
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')