Use my randomtimestamp module. It has 3 functions, randomtimestamp, random_time, and random_date.
Below is the signature of randomtimestamp function. It can generate a random timestamp between two years, or two datetime objects (if you like precision).
There's option to get the timestamp as a datetime object or string. Custom patterns are also supported (like strftime)
randomtimestamp(
start_year: int = 1950,
end_year: int = None,
text: bool = False,
start: datetime.datetime = None,
end: datetime.datetime = None,
pattern: str = "%d-%m-%Y %H:%M:%S"
) -> Union[datetime, str]:
Example:
>>> randomtimestamp(start_year=2020, end_year=2021)
datetime.datetime(2021, 1, 10, 5, 6, 19)
>>> start = datetime.datetime(2020, 1, 1, 0, 0, 0)
>>> end = datetime.datetime(2021, 12, 31, 0, 0, 0)
>>> randomtimestamp(start=start, end=end)
datetime.datetime(2020, 7, 14, 14, 12, 32)
Why not faker?
Because randomtimestamp is lightweight and fast. As long as random timestamps are the only thing you need, faker is an overkill and also heavy (being feature rich).