Matt Andrzejczuk
Matt Andrzejczuk

Reputation: 2066

How to convert yyyy-mm-dd hh:mm:ss to UNIX timestamp?

I'm trying to get the latest 350 images of a point using the python library which calls the Instagram API.

Here's what I've got so far:

from instagram import *
import time

lat = 40.7590615
lng = -73.969231

token = '1481551830.e7692be.f708e3beb2334bcd930f1d3feccc7b0d'
api = client.InstagramAPI(access_token = token)

media_list = api.media_search(None, count, lat, lng, None, None)
for item in media_list:
    print item.created_time

OUTPUT:

2014-10-14 03:37:56
2014-10-14 03:37:19
2014-10-14 03:36:46
2014-10-14 03:36:45
2014-10-14 03:36:38
2014-10-14 03:36:16
2014-10-14 03:36:09
2014-10-14 03:35:40
2014-10-14 03:35:28
2014-10-14 03:35:23
2014-10-14 03:35:17
2014-10-14 03:34:49
2014-10-14 03:34:02
2014-10-14 03:33:44
2014-10-14 03:33:41
2014-10-14 03:33:35
2014-10-14 03:33:21
2014-10-14 03:33:17
2014-10-14 03:33:05
2014-10-14 03:32:38
2014-10-14 03:32:09
2014-10-14 03:32:06
2014-10-14 03:31:50
2014-10-14 03:31:33
2014-10-14 03:31:14

So I almost have everything that I need but the output I'm getting is incompatible with the instagram API function:

api.media_search(None, count, lat, lng, MAX_TIMESTAMP, MIN_TIMESTAMP)

Where both MAX_TIMESTAMP and MIN_TIMESTAMP are Unix timestamps, I need to be able to convert the oldest timestamp 2014-10-14 03:31:14 into a Unix timestamp so that I can get more output from the instagram media search.

In quick python code how do I convert 2014-10-14 03:31:14 into Unix timestamp?

Upvotes: 0

Views: 2221

Answers (2)

Siniseus
Siniseus

Reputation: 121

Not sure if this is 100% correct but you can try using mktime and just convert it using timetuple.

unix_timestamp = time.mktime(item.created_time.timetuple())
print unix_timestamp

Upvotes: 0

BlackMamba
BlackMamba

Reputation: 10254

from datetime import datetime, timedelta

def totimestamp(dt, epoch=datetime(1970,1,1)):
    td = dt - epoch
    # return td.total_seconds()
    return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 1e6 

now = datetime.utcnow()
print now
print totimestamp(now)

You can try this.

Upvotes: 1

Related Questions