Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting time to epoch (Python) [duplicate]

I am using tweepy to get a tweet. I am trying to send this tweet over Slack. Tweepy gives the time in a strange format, and Slack requires the time in epoch.

for i in tweets:
    created=(i.created_at)
    print(created)
    print(created.strftime('%s'))

This returns

2017-01-17 14:36:26

Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\slackbot3\run.py", line 287, in main
print(created.strftime('%s'))
ValueError: Invalid format string

How can I get 2017-01-17 14:36:26 into epoch time?

like image 774
user2607110 Avatar asked Jan 17 '17 14:01

user2607110


1 Answers

You have to convert the string to time tuple -by appropriate spec, and then time tuple to EPOCH time. In Python it's a little bit awkward

import time
time_tuple = time.strptime('2017-01-17 14:36:26', '%Y-%m-%d %H:%M:%S')
time_epoch = time.mktime(time_tuple)

The result is 1484656586.0

like image 61
volcano Avatar answered Sep 17 '22 04:09

volcano