Discuss / Python / 作业

作业

Topic source

test_number

#1 Created at ... [Delete] [Delete and Lock User]

import re from datetime import datetime, timezone, timedelta

def to_timestamp(dt_str, tz_str):

#str转化datetime
dt = datetime.strptime(dt_str,'%Y-%m-%d %H:%M:%S')
#获取时区
dtutc=int(re.match('UTC([\+\-]\d{1,2}):\d{2}',tz_str).group(1))
#创建时区
utctime=timezone(timedelta(hours=dtutc))
#强制设置时区
utcdt=dt.replace(tzinfo=utctime)
#datetime转换为timestamp
ts=utcdt.timestamp()
return ts

d='2015-1-21 9:01:30' t='UTC+5:00' print(to_timestamp(d,t))


  • 1

Reply