Skip to content

Instantly share code, notes, and snippets.

@maurobaraldi
Last active August 29, 2015 13:58
Show Gist options
  • Save maurobaraldi/10024128 to your computer and use it in GitHub Desktop.
Save maurobaraldi/10024128 to your computer and use it in GitHub Desktop.
Closest datetime from now in list
from datetime import datetime as dt, time
from random import choice
from time import mktime
start = dt.combine(dt.now().date(), time(0, 0, 0))
end = dt.combine(start, time(23, 59, 59))
ts = lambda t: int(mktime(t.timetuple()))
today = range(ts(start), ts(end))
now = ts(dt.now())
#print len(today)
_today_ = []
for i in range(50):
c = choice(today)
_today_.append(c)
today.pop(today.index(c))
index, timestamp = min(enumerate(_today_), key=lambda x: abs(x[1]-now))
#print dt.fromtimestamp(timestamp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment