python3时间相关

1: 时区转换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from dateutil import parser
from dateutil import tz
from datetime import datetime


def utc_to_china(utc_ts_str):
dt = parser.parse(utc_ts_str)
to_zone = tz.gettz('Asia/Shanghai')
return dt.astimezone(to_zone).timestamp()

if __name__ == "__main__":

ts = utc_to_china("2019-03-11T00:56:00.000Z")
tt = datetime.fromtimestamp(ts).strftime("%Y-%m-%m %H:%M:%S")
print(tt)

打印结果:2019-03-03 08:56:00

坚持原创技术分享,您的支持将鼓励我继续创作!