import re
from datetime import datetime, timezone, timedelta
def to_timestamp(dt_str, tz_str):
#str to datetime
dt_datetime = datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S')
# print(dt_datetime)
# match timezone
tzonestr = re.match(r'(UTC)((\+|\-)\d+)(:\d+)', tz_str).group(2)
tzone = (int)(tzonestr)
# print(tzone, tzonestr)
# get finally datetime
end_dt = dt_datetime + timedelta(hours=8-tzone)
# print(end_dt)
# test demo
# t = 1433121030.0
# print(datetime.fromtimestamp(t))
return end_dt.timestamp()
print(to_timestamp('2015-5-31 16:10:30', 'UTC-09:00'))
python学习-时间戳
时间转换与处理
最新推荐文章于 2025-10-24 00:15:00 发布
1334

被折叠的 条评论
为什么被折叠?



