背景
在服务器上运行Python代码,发现时区不是中国标准时间(China Standard Time)
在服务中查看时间:timedatectl,发现local time是CST
不同服务器的时区不太一样,导致获取本地时间不一致
Local time: 二 2021-04-13 14:15:39 CST
Universal time: 二 2021-04-13 06:15:39 UTC
RTC time: 二 2021-04-13 14:15:39
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: yes
DST active: n/a
解决方法
import datetime
import pytz
run_time = datetime.datetime.now(pytz.timezone('CST')).strftime("%Y-%m-%d %H:%M:%S")
# PRC为北京时间 CST为中国标准时间