将 UNIX 时间戳转换为秒数

Convert UNIX timestamp to how long it was in seconds

我正在做一个项目,我需要帮助。

我想计算一张 Instagram 图片是在多长时间前发布的。使用他们的 json api 我可以举个例子:

      "edge_owner_to_timeline_media": {
    "count": 19938,
    "page_info": {
      "has_next_page": true,
      "end_cursor": "AQCJsPghTApTJTITHUm_iXp9fqZGXrfJXIYCc60OqvLM98v9a8xgDQ8hiIUUaViSd913BxRHSILvz8_G2Vfw0FALXEFEf2p2fpuNnupbvjqQ8A"
    },
    "edges": [
      {
        "node": {
          "__typename": "GraphImage",
          "id": "1825727462006678242",
          "edge_media_to_caption": {
            "edges": [
              {
                "node": {
                  "text": "Yep"
                }
              }
            ]
          },
          "shortcode": "BlWSXQdlWLi",
          "edge_media_to_comment": {
            "count": 9
          },
          "comments_disabled": false,
          "taken_at_timestamp": 1531863695,
          "dimensions": {
            "height": 1080,
            "width": 1080
          },

亮点是 "taken_at_timestamp",当图像以 UNIX 时间上传时,它会告诉我,但是我想做的是通过示例获取当前的 UNIX 时间:

import time
int(time.time()) 

然后以秒为单位计算从现在开始上传图像是在多长时间前(与时区无关)? 那可能吗?我很难理解 UNIX 和 Epoch times 以及所有这些:)

只需从 time.time() 中减去 taken_at_timestamp 即可得到两次之间的秒数之差。

import time

# I don't know your full json layout, so you will need to substitute this.
edges = data["edge_owner_to_timeline_media"]["edges"]

taken_at = edges[0]["taken_at_timestamp"]

age = time.time() - taken_at
print(age, "seconds.")