Sky*_*and 6 python datetime utc ipython
在IPython中:
Python 3.6.2 (default, Sep 4 2017, 13:34:02)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: datetime.utcnow().timestamp()
Out[1]: 1515396403.049774
Run Code Online (Sandbox Code Playgroud)
在Python中:
Python 3.6.2 (default, Sep 4 2017, 13:34:02)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> datetime.utcnow().timestamp()
1515425220.215077
>>>
Run Code Online (Sandbox Code Playgroud)
IPython的结果似乎是在我的时区而不是UTC。
编辑:无需调用.timestamp()返回的值.utcnow()在两个终端中都相同。为什么会这样呢?
编辑2:
在常规的Python提示,对于价值观datetime.utcnow().timestamp()和datetime.utcnow().replace(tzinfo=timezone.utc).timestamp()不同。
Python 3.6.2 (default, Sep 4 2017, 13:34:02)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime, timezone
>>> datetime.utcnow().replace(tzinfo=timezone.utc).timestamp()
1515548420.130655
>>> datetime.utcnow().timestamp()
1515577235.982597
Run Code Online (Sandbox Code Playgroud)
在IPython中,这些值是相同的。
编辑3:
在IPython的now和utcnow返回相同。在Python中,now返回我的本地时间。
In [4]: datetime.utcnow()
Out[4]: datetime.datetime(2018, 1, 10, 1, 44, 55, 496083)
In [5]: datetime.now()
Out[5]: datetime.datetime(2018, 1, 10, 1, 45, 53, 811077)
Run Code Online (Sandbox Code Playgroud)
蟒蛇:
>>> datetime.datetime.now()
datetime.datetime(2018, 1, 9, 17, 46, 29, 754642)
>>> datetime.datetime.utcnow()
datetime.datetime(2018, 1, 10, 1, 46, 32, 865136)
Run Code Online (Sandbox Code Playgroud)
当datetime.timestamp()在 的时区原生实例上调用时datetime,它假定该值是本地日期时间。因此,永远不应该使用该代码datetime.utcnow().timestamp(),因为非本地时区幼稚datetime.utcnow()违反了该假设。
获取 UTC 时间戳的“旧”正确方法是:
datetime.now().timestamp()
更好的方法是避免天真的日期时间,使用时区感知版本,以前使用:
datetime.utcnow().replace(tzinfo=timezone.utc).timestamp()
或现在,因为datetime.utcnow()自 Python 版本 3.12 以来已被弃用,使用:
datetime.now(tz=timezone.utc).timestamp()
查看效果的一个相当简单的方法是在时区偏移非零的环境中使用以下代码:
from datetime import datetime, timezone
now1 = datetime.now()
now2 = datetime.utcnow()
now3 = datetime.now(tz=timezone.utc)
now1
now1.timestamp()
now2
now2.timestamp() # WRONG!
now3
now3.timestamp()
Run Code Online (Sandbox Code Playgroud)
这会产生如下输出:
>>> from datetime import datetime, timezone
>>> now1 = datetime.now()
>>> now2 = datetime.utcnow()
>>> now3 = datetime.now(tz=timezone.utc)
>>> now1
datetime.datetime(2023, 12, 16, 11, 50, 0, 979519)
>>> now1.timestamp()
1702749000.979519
>>> now2
datetime.datetime(2023, 12, 16, 17, 50, 0, 982995)
>>> now2.timestamp() # WRONG!
1702770600.982995
>>> now3
datetime.datetime(2023, 12, 16, 17, 50, 0, 983405, tzinfo=datetime.timezone.utc)
>>> now3.timestamp()
1702749000.983405
Run Code Online (Sandbox Code Playgroud)
从上面可以看出,当使用“旧”方式与 timezone-naive local datetime 时now1,timestamp()应用时区偏移量,从而产生 UTC 时间戳值。now3同样,使用时区感知日期时间的“更好”方式timestamp()会产生相同的结果。请注意,如果没有时区信息, 的值与now2的值没有太大区别,now1只是提前了 6 小时。由于在调用时now2已经应用了一次时区偏移,因此调用 timezone-naive 值会再次应用时区偏移,从而产生一个时间戳值,其中时区偏移已应用两次!在上面的示例中,时区位于 UTC 以西 6 小时,导致时间戳实际上比本地时间早 12 小时,因此错误地比UTC 早 6 小时(21,600 秒)。utcnow()timestamp()