Search code examples
twittertweepytwitter-oauth

How To Convert "created_timestamp" Value To A Valid Date In Python


I'm currently working on a Twitter bot that automatically reply messages, I'm doing this by using tweepy (the official python twitter library)

I need to filter messages based on the created time as I don't want to reply same message twice. Now the problem is that the API endpoint returns created_timestamp as string representation of positive integers.

Below is an example of data returned as per the doc

{
  "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", ... },
    ...
  ]
}

My question is "How do I convert the created_timestamp to a valid date using python" ?.


Solution

  • You might play with timestamps on this resource

    And in your case could use methods like:

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

    From the datetime standard library. But integers after the first line are already well comparable if the task is to distinguish differences between the timestamps.