Last active
October 18, 2018 17:56
-
-
Save olivx/68ef118abb972b1b5cf9ade5e53e9cd8 to your computer and use it in GitHub Desktop.
This function will return a random datetime between two datetime objects.
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
from random import randrange | |
from datetime import timedelta | |
from datetime import datetime | |
import pytz | |
def random_date(start, end): | |
""" | |
This function will return a random datetime between two datetime | |
objects. | |
""" | |
delta = end - start | |
int_delta = (delta.days * 24 * 60 * 60) + delta.seconds | |
random_second = randrange(int_delta) | |
return start + timedelta(seconds=random_second) | |
local_tz = pytz.timezone('America/Sao_Paulo') | |
start = local_tz.localize(datetime(2018,5,3), is_dst=None) | |
end = datetime.utcnow().replace(tzinfo=pytz.utc) | |
print(random_date(start, end)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment