Discuss / Python / 交作业

交作业

Topic source

风摧落木

#1 Created at ... [Delete] [Delete and Lock User]
# -*- coding:utf-8 -*-

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')
    tz_re = re.match(r'^UTC([+-]\d+)\:\d{2}$',tz_str)
    tz_hours = int(tz_re.group(1))
    return cday.replace(tzinfo=timezone(timedelta(hours=tz_hours))).timestamp()


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

print('Pass')

  • 1

Reply