【问题标题】:datetime strftime does not output correct timestampdatetime strftime 不输出正确的时间戳
【发布时间】:2013-07-02 18:24:15
【问题描述】:

以下内容:

>>> from dateutil.parser import parse
>>> parse("2013-07-02 00:00:00 -0000")
datetime.datetime(2013, 7, 2, 0, 0, tzinfo=tzutc())

显示时间应为 UTC 时间 2013 年 7 月 2 日凌晨 12 点。

但是:

>>> parse("2013-07-02 00:00:00 -0000").strftime("%s")
'1372744800'

1372744800 实际上是 Tue, 02 Jul 2013 06:00:00 UTC,这是错误的。很困惑。

【问题讨论】:

  • 你从哪里导入parse
  • 无法复制;我收到1372741200(一小时前)。很确定这是一个时区问题。
  • 那仍然是错误的,它只是在不同的 TZ 中。更奇怪的是:parse("2013-07-02 00:00:00 -0000").strftime("%s %z") 会给你“1372744800 +0000”,这意味着 strftime 认为它是 UTC。
  • Python bug tracker 相关问题:datetime.strftime('%s') should respect tzinfo

标签: python datetime python-dateutil


【解决方案1】:

看到这个问题:Convert python datetime to epoch with strftime

Python 实际上并不支持 %s 作为 strftime 的参数(如果你 检查在 http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior 它不在列表中),它工作的唯一原因是因为 Python 正在将信息传递给您系统的 strftime,它使用您的 当地时区。

【讨论】:

  • 谢谢,成功了。人们说 python 是一种比 PHP(我来自哪里)优越得多的语言,但在 PHP 中使用 DateTime 时,这不会那么困难或令人困惑。
  • 您应该结帐pytzdelorean。这些使得使用日期时间很容易。还有check out this thread on reddit!
猜你喜欢
  • 1970-01-01
  • 2018-09-13
  • 1970-01-01
  • 2020-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-25
相关资源
最近更新 更多