Skip to content

Instantly share code, notes, and snippets.

@jstncno
Created August 24, 2015 22:33
Show Gist options
  • Select an option

  • Save jstncno/7226421c4ee58a35149b to your computer and use it in GitHub Desktop.

Select an option

Save jstncno/7226421c4ee58a35149b to your computer and use it in GitHub Desktop.
Random Python timestamp
# http://stackoverflow.com/questions/553303/generate-a-random-date-between-two-other-dates#answer-553320
import random, time
def str_time_prop(start, end, format, prop):
'''Get a time at a proportion of a range of two formatted times.
start and end should be strings specifying times formated in the
given format (strftime-style), giving an interval [start, end].
prop specifies how a proportion of the interval to be taken after
start. The returned time will be in the specified format.
'''
stime = time.mktime(time.strptime(start, format))
etime = time.mktime(time.strptime(end, format))
ptime = stime + prop * (etime - stime)
return time.strftime(format, time.localtime(ptime))
def random_date(start, end, prop):
return str_time_prop(start, end, '%Y-%m-%d %H:%M:%S', prop)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment