Randomly Frozen Time Function

  • Share this:

Code introduction


This function uses the freezegun library to freeze the time to a randomly selected past date and time.


Technology Stack : Freezegun, Python

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
from freezegun import freeze_time
import datetime
import time

def random_freeze_time():
    """
    This function freezes the time to a random past date and time.
    """
    frozen_time = random.choice([
        "2021-01-01 12:00:00",
        "2020-06-15 08:30:00",
        "2019-12-25 16:45:00",
        "2018-04-20 21:00:00",
        "2017-08-10 14:15:00"
    ])
    with freeze_time(frozen_time):
        print("Frozen time is:", frozen_time)

# JSON Explanation