Discuss / Python / 没用正则,跳着看的

没用正则,跳着看的

Topic source

蛋花_Smile

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

import re
from datetime import datetime, timedelta, timezone

def to_timestamp(dt_str,tz_str):
    dt = datetime.strptime(dt_str,'%Y-%m-%d %H:%M:%S')
    print(dt)
    tz = tz_str.split(':')[0].replace('UTC','')
    print(tz)
    print(int(tz))
    # 时区之后
    tz_dt = dt.replace(tzinfo=timezone(timedelta(hours=int(tz))))
    print(tz_dt)
    # 时间戳
    dt_ts = tz_dt.timestamp()
    print(dt_ts)
    return dt_ts

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('ok')

  • 1

Reply