python - 将 UNIX 时间戳转换为以秒为单位的时间长度
问题描述
我正在做一个项目,我需要帮助。
我想计算多久前发布了一张 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 时代以及所有这些 :)
解决方案
只需减去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.")
推荐阅读
- ruby-on-rails - Rack::QueryParser::InvalidParameterError(参数无效编码:f�u6�]��S��7��&A�y��):
- regex - 如何编写接受 1 到 25000 之间的数字的正则表达式
- java - 在 jax rs jersey 中发帖无法正常工作
- amazon-web-services - 我可以将共享托管与 Azure 与 Amazon 一起使用吗
- typescript - 断言类型必须匹配接口,但保留原始类型
- c# - 如何从 C# ValidationRule 访问 WPF XAML GUI 元素?
- spring-boot - IntelliJ Idea 无法使用 gradle 运行 spring boot
- java - 如何修改 apk 以从启动器中隐藏?
- python-3.x - 什么是 netstat –nb 以及如何在 python 中使用它?
- uwp - 用于 UWP 的 Google API 在自定义重定向方案中表现不佳