Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  "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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading