为什么utcnow关闭1小时?
代码:
from datetime import *
print datetime.utcnow()
结果:
2016-11-25 10:28:04.655978
Servertime:
Fri Nov 25 11:28:11 CET 2016
答案 0 :(得分:3)
因为CET是UTC + 1
点击此链接查看转化: http://www.worldtimebuddy.com/utc-to-cet-converter
答案 1 :(得分:1)
datetime.utcnow()
返回UTC(协调世界时)时间:UTC Time definition
与服务器同时使用datetime.now()
。
In [8]: from datetime import datetime
In [9]: datetime.utcnow()
Out[9]: datetime.datetime(2016, 11, 25, 10, 32, 18, 288195)
In [10]: datetime.now()
Out[10]: datetime.datetime(2016, 11, 25, 11, 32, 22, 336213)
答案 2 :(得分:0)
发生这种情况是因为您的服务器处于不同的时区。 (CET = +1)
>>> from datetime import datetime
>>>
>>> datetime.utcnow()
datetime.datetime(2016, 11, 25, 10, 35, 33, 931951)
>>>
使用pytz
模块更改时区
pip install pytz
答案 3 :(得分:0)
您也可以使用时区,如
from datetime import datetime
from pytz import timezone
fmt = "%Y-%m-%d %H:%M:%S %Z%z"
# Current time in UTC
now_utc = datetime.now(timezone('UTC'))
print now_utc.strftime(fmt)
# Convert to US/Pacific time zone
now_pacific = now_utc.astimezone(timezone('US/Pacific'))
print now_pacific.strftime(fmt)
# Convert to Europe/Berlin time zone
now_berlin = now_pacific.astimezone(timezone('Europe/Berlin'))
print now_berlin.strftime(fmt)