Last active
August 29, 2015 13:58
-
-
Save maurobaraldi/10024128 to your computer and use it in GitHub Desktop.
Closest datetime from now in list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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