# -*- coding:utf-8 -*- import re,string from datetime import datetime, timezone, timedelta def to_timestamp(dt_str, tz_str): timetemp=datetime.strptime(dt_str,'%Y-%m-%d %H:%M:%S') ret=re.match(r'^UTC([+|-])(\d+):00$',tz_str) tzn=(int)(ret.group(2)) if ret.group(1)=='+': tz=tzn elif ret.group(1)=='-': tz=-1*tzn restt=timetemp.replace(tzinfo=timezone(timedelta(hours=tz))) return restt.timestamp() t1 = to_timestamp('2015-6-1 08:10:30', 'UTC+7:00') print(t1) 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
Sign in to make a reply
正版琅琊