Discuss / Python / 考虑非整点时区

考虑非整点时区

Topic source

zhu_weimin123

#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): time=datetime.strptime(dt_str,'%Y-%m-%d %H:%M:%S') tz=re.match(r'^UTC([+|-][\d]+)(:)([\d]+)',tz_str) tz_input=float(tz.group(1))+float(tz.group(3))/60 tz_utc_8=timezone(timedelta(hours=tz_input)) newtime=time.replace(tzinfo=tz_utc_8) return newtime.timestamp()

测试:

t1 = to_timestamp('2015-6-1 08:10:30', 'UTC+7:30') assert t1 == 1433119230.0, t1

t2 = to_timestamp('2015-5-31 16:10:30', 'UTC-09:00') assert t2 == 1433121030.0, t2

print('ok')

Lllliaaa

#2 Created at ... [Delete] [Delete and Lock User]

这段是不是还是存在一个bug,就是当时区是负非整数的时候。比如-9:20,时区转换就成了(-9)+(20/60)


  • 1

Reply