You can download this code by clicking the button below.
This code is now available for download.
This function allows the user to specify a date range and then randomly freeze time to a random date within that range. This can be used to simulate time-related features in test environments.
Technology Stack : Freezegun, datetime
Code Type : Python Function
Code Difficulty : Intermediate
import random
from freezegun import freeze_time
import datetime
def random_freeze_time(arg1, arg2):
"""
This function will freeze time to a random datetime within a specified range.
:param arg1: The start date for the range.
:param arg2: The end date for the range.
:return: None
"""
start_date = datetime.datetime.strptime(arg1, "%Y-%m-%d")
end_date = datetime.datetime.strptime(arg2, "%Y-%m-%d")
random_date = start_date + (end_date - start_date) * random.random()
freeze_time(random_date)