【问题标题】:Converting unix timestamp (length 13) string to readable date in Python在 Python 中将 unix 时间戳(长度 13)字符串转换为可读日期
【发布时间】:2017-04-10 08:35:50
【问题描述】:

我正在尝试将此 unix 时间戳 1491613677888 转换为可读日期。 在这里(stackoverflow)找到了那个python脚本:

import datetime
print(
    datetime.datetime.fromtimestamp(
    int("1284101485")
    ).strftime('%Y-%m-%d %H:%M:%S')
)

但是当我把时间戳放在那里时,我得到了那个错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument

现在我看到我使用的时间戳长了 3 个字符。 我在这个链接上检查了它: http://www.unixtimestamp.com/index.php

并看到它得到了它的时间。 我怎么能用python做到这一点? (我使用的是 python 3.4)

【问题讨论】:

  • 无法重现(python2.7 + 3)
  • 你的时间戳中最后的 888 是从哪里得到的?第一部分1491613677 是 2017-04-08T01:07:57+00:00,这可能是你想要的。有了它,你的代码就能完美运行。
  • 你是从某个以毫秒为单位输出的函数中得到的吗?
  • 当我在 Python 2 上使用 int("1284101485888") 时,我得到 ValueError: year is out of range,在 Python 3.6 上我得到 42661-07-22 17:11 :28.
  • 关于@ThierryLathuille 评论。是的,我需要毫秒。我如何在 python 3.4 中使用它?

标签: python unix unix-timestamp


【解决方案1】:

您的timestamp 不是“经典”Unix 时间戳(自 1970 年 1 月 1 日以来的秒数),因为它以毫秒为单位。

你可以这样翻译:

import datetime

timestamp_with_ms = 1491613677888

# We separate the 'ordinary' timestamp and the milliseconds
timestamp, ms = divmod(timestamp_with_ms, 1000)
#1491613677 888

# We create the datetime from the timestamp, we must add the 
# milliseconds separately
dt = datetime.datetime.fromtimestamp(timestamp) + datetime.timedelta(milliseconds=ms)


formatted_time = dt.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
# With Python 3.6, you could use:
# formatted_time = dt.isoformat(sep=' ', timespec='milliseconds')

print(formatted_time)
# 2017-04-08 03:07:57.888

编辑:我没有注意到 fromtimestamp 接受浮点数。所以,我们可以简单地这样做:

import datetime
timestamp_with_ms = 1491613677888

dt = datetime.datetime.fromtimestamp(timestamp_with_ms / 1000)

formatted_time = dt.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
# With Python 3.6, you could use:
# formatted_time = dt.isoformat(sep=' ', timespec='milliseconds')

print(formatted_time)
# 2017-04-08 03:07:57.888

【讨论】:

    【解决方案2】:

    您的时间戳长 3 个字符并且是标准的 unix 时间戳?这意味着您的时间戳至少是从今天开始的 40,000 年。否则,最后 3 个字符可能代表其他内容,例如毫秒,但这并不能解释您看到的错误。

    如果它们是毫秒,并且看到您没有在格式字符串中使用它们,我认为简单地剥离它们没有什么害处。

    standard_unix_ts = int("1284101485000"[:-3])
    

    编辑考虑到@cdarke 的评论,我建议改为:

    standard_unix_ts = int("1284101485000"[:10])
    

    编辑 2 关注 Gils 评论

    import datetime
    
    not_unix_ts = "1284101485088"
    unix_ts, milliseconds = not_unix_ts[:10], not_unix_ts[10:]
    dt = datetime.datetime.fromtimestamp(float(unix_ts))
    FORMAT_STRING = '%Y-%m-%d %H:%M:%S'
    print("%s and %s milliseconds" % (dt.strftime(FORMAT_STRING), milliseconds))
    

    【讨论】:

    • 只是一个想法,如果有正确长度的时间戳,取前 10 个字符可能比去掉后 3 个字符更安全。
    • 非常有效的评论,我完全同意。
    • 在我添加的链接中,我看到了毫秒 - 我需要它们。该时间戳是密钥的一部分。我需要这一切。那么我怎样才能在 python 3.4 中获得毫秒呢?
    • 简单地将它们从字符串中剥离出来,为您提供一个实时时间戳,最后 3 个字符代表您的毫秒数。重新迭代:unix 时间戳包含毫秒,因此库在解析它们时遇到问题是可以理解的。我的答案可以更好地证明这一点。
    猜你喜欢
    • 2022-01-21
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 2019-07-30
    • 2012-06-23
    • 2011-09-23
    • 1970-01-01
    相关资源
    最近更新 更多