Discuss / Python / 作业

作业

Topic source

import re from datetime import datetime,timezone,timedelta

def to_timestamp(dt_str,tz_str): cday = datetime.strptime(dt_str,'%Y-%m-%d %H:%M:%S')

# print(int(tz_str.split('UTC')[1].split(':')[0]))
tz_utc = timezone(timedelta(hours=int(tz_str.split('UTC')[1].split(':')[0])))

return cday.replace(tzinfo=tz_utc).timestamp()

print(to_timestamp('2015-6-1 18:19:59','UTC-5:00'))

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


  • 1

Reply