'How properly use `unittest.mock`?

I have this legacy function, that I would like to write a unit test:

def get_hour_diff(src):
    if not src:
        return 0
    return ((datetime.strptime(src, "%Y%m%d%H%M%SZ") - datetime.now()).total_seconds()) / 3600 if src else 0

How can I mock datetime.now() using standard library unittest?
I managed to do it using Freezegun decorator @freeze_time("2022-01-01") on my test function, and it works perfectly fine, but I wonder how could this be done without external packages.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source