-- coding:utf-8 --
import re from datetime import datetime, timezone,timedelta
def to_timestamp(dt_str,tz_str): d = datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S')
regStr = r'^UTC(\+|-{1})(0*\d*|1[0-2]*):00' pattern = re.compile(regStr) m = pattern.match(tz_str) tz_neg_str = m.groups(2)[0] tz_num_str = m.groups(2)[1] tz_num = int(tz_num_str) if tz_neg_str == '-': tz_num = tz_num * -1 tz_utc = timezone(timedelta(hours=tz_num)) utc_dt = d.replace(tzinfo = tz_utc) print(utc_dt) return utc_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
lubang03
import re from datetime import datetime, timezone,timedelta
def to_timestamp(dt_str,tz_str): d = datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S')
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')