# -*- coding: utf-8 -*- import re from datetime import datetime, timezone, timedelta def to_timestamp(df_str, tz_str): cday = datetime.strptime(df_str, '%Y-%m-%d %H:%M:%S') tz_hour = re.match(r'^(UTC)([+-]d{1,2})[:](d{2})$', tz_str) tz_utc_x = timezone(timedelta(hours=int(tz_hour.group(2)))) utc_time11 = cday.replace(tzinfo=tz_utc_x) return (utc_time11.timestamp()) # if __name__ == '__main__': # t1 = to_timestamp('2015-6-1 08:10:30', 'UTC+7:00') # print(t1 == 1433121030.0) # t2 = to_timestamp('2015-5-31 16:10:30', 'UTC-09:00') # print(t2 == 1433121030.0) # 测试: 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')
https://www.liaoxuefeng.com/wiki/1016959663602400/1017648783851616