声明

本文章只针对python3.6及以上版本。


问题提出

首先,我们先import一些必要模块:

In [1]: from datetime import datetime, timezone, timedelta

接下来,看下面这一段令人疑惑的代码:

In [2]: dt = datetime(1970, 1, 1, 0, 0)

In [3]: print(dt)
1970-01-01 00:00:00 In [4]: dt.timestamp()
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-4-76b27e848bf3> in <module>()
----> 1 dt.timestamp() OSError: [Errno 22] Invalid argument In [5]: dtime = datetime(1970, 1, 2, 8, 0) In [6]: print(dtime)
1970-01-02 08:00:00 In [7]: dtime.timestamp()
Out[7]: 86400.0

首先,我们知道,在计算机中,时间实际上是用数字表示的。我们把1970年1月1日 00:00:00 UTC+00:00时区的时刻称为epoch time,记为0(1970年以前的时间timestamp为负数),当前时间就是相对于epoch time的秒数,称为timestamp。

这里,我构建了一个“1970年1月1日 00:00:00”的时间,按照理解,这个时间的timestamp应该是0.但从输出可以看到,出现了OSError错误。

经过一点点探索,我发现,能够以上面方式输出timestamp的最小时间是“1970年1月2日 08:00:00”,也就是说,如果dtime = datetime(1970, 1, 2, 7, 0)时,dtime.timestamp()也会引起OSError错误。

我十分疑惑的地方有三个:

  1. 为什么In [4]: dt.timestamp()会出现错误?
  2. 为什么能够输出timestamp的最小时间是datetime(1970, 1, 2, 8, 0)
  3. 为什么In [7]: dtime.timestamp()的输出是24小时,而不是24+8小时?

显式加入时区看一看

In [3]: print(dt)的输出来看,我们发现,用类似于datetime(1970, 1, 1, 0, 0)这样的语句,是隐式采用系统所在时区的。现在,我们来显式指定时区:

In [8]: dt0 = dt.replace(tzinfo = timezone(timedelta(hours = 0)))

In [9]: print(dt0)
1970-01-01 00:00:00+00:00 In [10]: dt0.timestamp()
Out[10]: 0.0 In [11]: dt8 = dt.replace(tzinfo = timezone(timedelta(hours = 8))) In [12]: print(dt8)
1970-01-01 00:00:00+08:00 In [13]: dt8.timestamp()
Out[13]: -28800.0 In [14]: dt_8 = dt.replace(tzinfo = timezone(timedelta(hours = -8))) In [15]: print(dt_8)
1970-01-01 00:00:00-08:00 In [16]: dt_8.timestamp()
Out[16]: 28800.0

这里,需要说一下背景知识:

以伦敦格林尼治天文台所在为本初子午线,定为零时区(中时区)。向东为+时区,向西为-时区。

replace函数显式地强制地将时区转换为tzinfo参数所指定地时区。此时,你应该注意In [12]: print(dt8)的输出,dt8是从dt转换过来地,dt是1970-01-01 00:00:00,dt8也是这个时间,但后面多了一点东西:+08:00

我用dt8表示东八区,即北京所在时区,用dt0表示中时区,即伦敦所在时区,用dt_8表示西八区。

好了,来解释代码:

  1. 我们从In [9]: print(dt0)的输出中看到+00:00,这代表dt0的意思是中时区的1970-01-01 00:00:00,我们都知道,timestamp是相对于epoch time的秒数,Out[10]: 0.0证明了这一点。
  2. In [12]: print(dt8)的输出中看到+08:00,这代表dt8的意思是东八区的1970-01-01 00:00:00。东八区比中时区早8个小时,所以,由于timestamp是相对于epoch time的秒数,所以dt8.timestamp()应该是-8小时,Out[13]: -28800.0验证了这一点。
  3. 接下来的dt_8就很容易理解了,和dt8是一样道理。dt_8是西八区,比中时区少8小时,所以要加8小时才能“到达”中时区。Out[16]: 28800.0验证了这一点。

其实,到了这里,我们已经可以解答上面提出的第三个疑惑了:为什么In [7]: dtime.timestamp()的输出是24小时,而不是24+8小时? **

我们定义的时间是01-02 08:00,按照惯性思维01-02 08:00-01-01 00:00等于24+8小时,没毛病呀?为什么结果不是这样呢?

真正的原因是:

dtime是东八区的时间,换算为中时区的时间应该是1970-01-02 00:00。所以,1970-01-02 00:00-1970-01-01 00:00等于24小时。总之,你要时刻记住:
timestamp是相对于epoch time的秒数**。


解决最后的问题

其实,要回答最后的问题,只需要知道一点点东西就够了,那便是显式指定时区和隐式采用系统时区的区别。

我们从回答问题开始:为什么能够输出timestamp的最小时间是datetime(1970, 1, 2, 8, 0)

隐式采用系统所在时区时,类似于dtime.timestamp()这样的输出不可以有负数。这是为了应对在实际应用中世界各地的时间能够统一。那么,怎么能让timestamp不为负数呢?就是相对于中时区,从1970-01-02 00:00开始算起,这是中时区的最小时间。这样的话,我无论向东还是向西都不会出现负数。由于,我们用的时北京时间,即东八区,所以,最小时间是1970-01-02 08:00

同样道理,如果我们在东一区,那么我们的最小时间是1970-01-02 01:00,为了验证,我开了虚拟机,并且把时区调到东一区,下面是输出:

>>> dt = datetime(1970, 1, 2, 1, 0)
>>> print(dt)
1970-01-02 01:00:00
>>> dt.timestamp()
86400.0

现在只剩下最后的问题了:

为什么In [4]: dt.timestamp()会出现错误?

到了这里,我们可以看官方文档的东西了。我截取官方的一段话:

There is no method to obtain the POSIX timestamp directly from a naive datetime instance representing UTC time. If your application uses this convention and your system timezone is not set to UTC, you can obtain the POSIX timestamp by supplying tzinfo=timezone.utc

大意是我们无法直接从原生的datetime实例中直接获取代表utc时间的timestamp,你可以通过应用tzinfo=timezone.utc来获取timestamp。

注意:还记得我们前面说过的这句话吗:

replace函数"显式地"强制地将时区转换为tzinfo参数所指定地时区。

这里的原生的datetime实例其实已经隐式地采用了你系统所在时区了。看代码:

>>> dtest = datetime(1970, 1, 2, 8, 0)
>>> dtest1 = dtest.replace(tzinfo = timezone(timedelta(hours = 8)))
>>> dtest.timestamp()
86400.0
>>> dtest1.timestamp()
86400.0
>>> dtest2 = dtest.replace(tzinfo = timezone(timedelta(hours = 0)))
>>> dtest2.timestamp()
115200.0

dtest和dtest1的输出是一样的,这里说明了如果不显式指定系统时区,就隐式采用系统所在时区。这里,建议你往上拉一下,再看一看显式指定和隐式采用系统所在时区的区别。隐式时要从1970-01-02 +时区算起,即不能出现负数。

现在,我们指定tzinfo的参数,发现dt能输出了。

In [17]: dt
Out[17]: datetime.datetime(1970, 1, 1, 0, 0) In [18]: print(dt)
1970-01-01 00:00:00 In [19]: dt.replace(tzinfo = timezone.utc).timestamp()
Out[19]: 0.0 In [20]: dt.timestamp()
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-20-76b27e848bf3> in <module>()
----> 1 dt.timestamp() OSError: [Errno 22] Invalid argument

结尾

文章有点啰嗦。要说明的一点是:所谓的显式和隐式是为了帮助我理解概念我自己写的。其实如果你是位很聪明且基础好的人,看到下面这段话就已经全明白了:

Changed in version 3.6: The timestamp() method uses the fold attribute to disambiguate the times during a repeated interval.There is no method to obtain the POSIX timestamp directly from a naive datetime instance representing UTC time. If your application uses this convention and your system timezone is not set to UTC, you can obtain the POSIX timestamp by supplying tzinfo=timezone.utc


版权:保留所有权,转载注明请出处


python中时间戳的探索的更多相关文章

  1. python中时间戳,datetime 和时间字符串之间得转换

    # datetime时间转为字符串def Changestr(datetime1):    str1 = datetime1.strftime('%Y-%m-%d %H:%M:%S')    retu ...

  2. Python 将时间戳转换为本地时间并进行格式化

    在python中,时间戳默认是为格林威治时间,而我们为东八区 使用localtime() 本地化时间戳 使用 strftime() 格式化时间戳 time = time.strftime('%Y%m% ...

  3. Python中变量的本质探索

    Python中变量的本质探索 参考:Vamei博客Python进阶09 动态类型 ''' a = [1,2,3] ''' (1)这条"赋值语句"实际上是将a指向对象"[1 ...

  4. python中常用的模块的总结

    1. 模块和包 a.定义: 模块用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件.(例如:文件名:test.py,对应的模块名:test) ...

  5. 关于Python中的文件操作(转)

    总是记不住API.昨晚写的时候用到了这些,但是没记住,于是就索性整理一下吧: python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Pyth ...

  6. python中几个常见的“黑盒子”之 列表list

    python常见的数据类型有:字符串,布尔类型,整数,浮点数,数字,日期,列表,元祖,字典.相信前面6个大家都非常的熟悉,但是对于python的列表,元祖,字典我有时候一直在想其内部的实现是怎么样子的 ...

  7. python 中time模块使用

    在开始之前,首先要说明这几点: 1.在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素.由于Python的time模块实现主 ...

  8. python中时间的基本使用

    格式化日期 我们可以使用 time 模块的 strftime 方法来格式化日期,: time.strftime(format[, t]) #!/usr/bin/python # -*- coding: ...

  9. 可爱的 Python : Python中函数式编程,第二部分

    英文原文:Charming Python: Functional programming in Python, Part 2,翻译:开源中国 摘要:  本专栏继续让David对Python中的函数式编 ...

随机推荐

  1. 【C语言】(for循环嵌套)找出1000以内的水仙花数

    什么是水仙花数? 水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153). 分析: 根据定义可知: a*a*a+b*b*b+c*c*c ...

  2. 安卓之视图View的基本属性

    一.在xml布局文件中常用的属性定义   (1)  id:指定该视图的编号   (2)  layout_width:指定该视图的宽度可以是具体的dp值:可以是match_parent,表示与上级视图一 ...

  3. NAT穿透的方式

    目前主要的NAT类型有如下几种: 1)Full-cone NAT, also known as one-to-one NAT 一旦一个内网地址 (iAddr:iPort) 被映射到一个外部地址 (eA ...

  4. ASP.NET Core搭建多层网站架构【5-网站数据库实体设计及映射配置】

    2020/01/29, ASP.NET Core 3.1, VS2019, EntityFrameworkCore 3.1.1, Microsoft.Extensions.Logging.Consol ...

  5. 绕过QQ群文件下载限速

    绕过QQ群文件下载限速 引言 众所周知,用QQ客户端下载QQ群文件,速度往往被限为10KB/s.这里我们来讲讲如何绕过这一限制. 原始事件发生在2020年2月2日,值武汉疫情爆发,全国各省市纷纷下令推 ...

  6. Vue 实现全局使用sass, less变量

    首先   运行这行命令 npm install sass-resources-loader --save-dev: 在项目的build/utils.js中,找到 function generateLo ...

  7. Codeforces Round #611 (Div. 3) E

    Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past ...

  8. 杭电 1772 cake

    Cake Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. 【layui】提交表单

    1 <script type="text/javascript"> layui.use(['form', 'layer', 'jquery'], function () ...

  10. IIS 配置迁移

    使用管理员身份运行cmd 应用程序池: # 导出所有应用程序池 %windir%\system32\inetsrv\appcmd list apppool /config /xml > c:\a ...