Skip to main content

freeze_time

Context manager to temporarily freeze time.time(). This temporarily modifies the time function of the time module. Modules which import the time function directly (e.g. from time import time) won't be affected This isn't meant as a public API, but helps reduce some repetitive code in Django's test suite.

def freeze_time(
t: float
) - > Generator

Context manager to temporarily freeze time.time(). This temporarily modifies the time function of the time module. Modules which import the time function directly (e.g. from time import time) won't be affected This isn't meant as a public API, but helps reduce some repetitive code in Django's test suite.

Parameters

NameTypeDescription
tfloatThe fixed Unix timestamp that time.time() should return during the duration of the context manager.

Returns

TypeDescription
GeneratorA context manager that yields control while the system time is mocked, then restores the original time function upon exit.