如何在 Python 中将"created_timestamp"值转换为有效日期



我目前正在开发一个自动回复消息的推特机器人,我使用tweepy(官方python推特库(

我需要根据创建的时间过滤消息,因为我不想回复同一条消息两次。现在的问题是API端点返回created_timestamp作为正整数的字符串表示。

以下是根据文档返回的数据示例

{
"next_cursor": "AB345dkfC",
"events": [
{ "id": "110", "created_timestamp": "1639919665615", ... },
{ "id": "109", "created_timestamp": "1639865141987", ... },
{ "id": "108", "created_timestamp": "1639827437833", ... },
{ "id": "107", "created_timestamp": "1639825389806", ... },
{ "id": "106", "created_timestamp": "1639825389796", ... },
{ "id": "105", "created_timestamp": "1639825389768", ... },
...
]
}

我的问题是";如何使用python将created_timestamp转换为有效日期?。

您可以在此资源上使用时间戳

在您的情况下,可以使用以下方法:

timestamp = int('timestamp_string')
datetime.fromtimestamp(timestamp, tz=None)
date.fromtimestamp(timestamp)

来自日期时间标准库。但是,如果任务是区分时间戳之间的差异,那么第一行之后的整数已经很好地可比较了。

最新更新