Discuss / Python / 交作业

交作业

Topic source

4ever

#1 Created at ... [Delete] [Delete and Lock User]
def to_timestamp(dt_str, tz_str):    # str转换为datetime    dt = datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S')    # 时差正则分组    regex = r'^(\w{3})(.\d{1,2})'    time_diff_regex = re.match(regex, tz_str)    # 取时差    time_diff = int(time_diff_regex.group(2))    # 设置时区    time_zone = timezone(timedelta(hours=time_diff))    # 根据时区转化时间    dt = dt.replace(tzinfo=time_zone)    # 返回时间戳    return dt.timestamp()# 测试:t1 = to_timestamp('2015-6-1 08:10:30', 'UTC+7:00')print(t1)assert t1 == 1433121030.0, t1t2 = to_timestamp('2015-5-31 16:10:30', 'UTC-09:00')print(t2)assert t2 == 1433121030.0, t2print('ok')

  • 1

Reply