Created
May 8, 2018 17:56
-
-
Save warborn/135156ecf2a64470943c694f0d95c152 to your computer and use it in GitHub Desktop.
Random Hour Generator
This file contains hidden or 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
def random_hour(start, end, hour_format="%H:%M"): | |
start_date = datetime.strptime( | |
'2018-01-01 {}'.format(start), '%Y-%m-%d %I:%M %p' | |
) | |
end_date = datetime.strptime( | |
'2018-01-01 {}'.format(end), '%Y-%m-%d %I:%M %p' | |
) | |
delta = end_date - start_date | |
int_delta = (delta.days * 24 * 60 * 60) + delta.seconds | |
random_second = randrange(int_delta) | |
date = start_date + timedelta(seconds=random_second) | |
return datetime.strftime(date, hour_format) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment