Search code examples
pythonapitwittertimetweets

python twitter api tweet timestamp convert from UTC to EST


Using the twitter api for python, I need to get the timestamp of the tweet. I did that. I just need to know how to convert it from UTC to EST.

here is the code:

def main():
    twitter = Twitter(auth=OAuth('....'))
    tweet = twitter.statuses.user_timeline.snl()
    tweet_datetime = tweet[0]['created_at']
    print tweet_datetime 


>>> main()
>>> Tue Jun 18 22:23:22 +0000 2013

Solution

  • Here's a solution using pytz module:

    from pytz import timezone
    from datetime import datetime
    
    
    eastern = timezone('US/Eastern')
    utc = timezone('UTC')
    
    created_at = datetime.strptime(tweet[0]['created_at'], '%a %b %d %H:%M:%S +0000 %Y')
    utc_created_at = utc.localize(created_at)
    print utc_created_at
    est_created_at = utc_created_at.astimezone(eastern)
    print est_created_at
    

    prints:

    2013-06-18 22:23:22+00:00
    2013-06-18 18:23:22-04:00
    

    Hope that helps.