Discuss / Python / 学习学习

学习学习

Topic source
import re
from datetime import datetime,timezone,timedelta
def tz_utc_from_str(str):
    rex=re.compile(r'UTC([+-]\d{1,2}):\d\d')
    result=rex.match(str).group(1)
    return result

def to_timestamp(dt_str,tz_str):
    t = datetime.strptime(dt_str,'%Y-%m-%d %H:%M:%S')
    tz_utc = timezone(timedelta(hours=int(tz_utc_from_str(tz_str))))
    dt = t.replace(tzinfo=tz_utc)
    d = dt.timestamp()
    return d

print(to_timestamp('2015-6-1 08:10:30','UTC+7:00'))
print(to_timestamp('2015-5-31 16:10:30', 'UTC-02:00'))

  • 1

Reply