Discuss / Python / 交作业

交作业

Topic source

落汐21

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

import re from datetime import datetime, timezone, timedelta

def to_timestamp(dt_str, tz_str): if re.match(r'^\d{4}-([1-9]|1[0-2])-([1-9]|[1-2][0-9]|3[0-1])\s+(0[0-9]|1[0-9]|2[0-3])\:([0-5][0-9])\:([0-5][0-9])$',dt_str)==None: raise IOError('输入的日期不正确') elif re.match(r'UTC(+|-)([0-9]|[0-1][0-9]|2[0-3])\:00$',tz_str)==None: raise IOError('输入的时区信息不正确') else : m=re.match(r'UTC(+|-)([0-9]|[0-1][0-9]|2[0-3])\:00$',tz_str) dt=datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S')+timedelta(hours=8) hour=int(m.group(2)) if tz_str[3]=='+': dt_stamp=dt-timedelta(hours=hour) return dt_stamp.timestamp() else: dt_stamp=dt+timedelta(hours=hour) return dt_stamp.timestamp()


  • 1

Reply