Python中处理小于10位数字的timestamp时遇到的问题

  • 之前一直没注意,原来fromtimestamp不能处理 10 位以内的时间。
  • utcfromtimestamp可以,但是不带时区。
  • 而且utcfromtimestamp支持负数,但只到 -43200,也就是半天,很微妙的数字。

满隐蔽的一个坑点。速度倒是 utcfromtimestamp 快很多。
那么问题来了,有啥能完美处理时间戳的方法嘛?

datetime.fromtimestamp(1234567890)
# 2009-02-14 07:31:30 东 8 区

datetime.fromtimestamp(0)

error

datetime.utcfromtimestamp(0)

1970-01-01 00:00:00 不含时区

datetime.utcfromtimestamp(1234567890)

2009-02-13 23:31:30 不含时区

datetime.utcfromtimestamp(-43200)

1969-12-31 12:00:00


Python中处理小于10位数字的timestamp时遇到的问题

4 回复

从来不看文档吗?
<br>fromtimestamp() may raise ValueError, if the timestamp is out of the range of values supported by the platform C localtime() or gmtime() functions. It ’ s common for this to be restricted to years in 1970 through 2038. <br>


我无法理解你的问题

厉害厉害,第一次碰到会看文档的大神。U •ェ• U

我操 我才知道原来可以这么用 我还是手动转换 localtime

回到顶部