Search code examples
twittertwitter4j

Is there a way to tell if one tweet was created after another based on the tweet id?


Currently I'm checking if a tweet was created after another tweet based on the timestamp, but this is proving inconsistent. What is working locally is not working all the time on my server. I suspect a timezone issue (my server is in another time zone).

But I was wondering if I can do this based on Twitter's unique ids for each tweet? Something in the form:

if (tweet2.id > tweet1.id) {
    // tweet2 created after tweet 1
} else {
    // tweet2 created before tweet 1
}

Is this possible? If not I'll ask another question about what could be going wrong with my date implementation.


Solution

    • Twitter ids are generated by twitter snowflake mechanism which is an algorithm and a cluster of machines to produce it. In general those id's are sequential so your comparison will basically hold. The problem is those id's are currently > 64 bits so js will truncate those. The trick is to use tweetXXX.id_str field which is the id casted to a string for your comparison.
    • I haven't see your date implementation but probably you are no comparing UTC date values. Any way comparison made on datetime will not give you correct results even on ms scale there will be many tweets on same date time.