将UNIX时间戳转换为秒

时间:2018-07-17 22:13:05

标签: python python-3.x unix time unix-timestamp

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

我想计算多久前发布了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时代以及所有这些:)

1 个答案:

答案 0 :(得分:2)

只需从taken_at_timestamp中减去time.time(),即可获得两秒之间的差值。

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.")
相关问题