diff --git a/freezegun/api.py b/freezegun/api.py index 43b117e..10a1fbe 100644 --- a/freezegun/api.py +++ b/freezegun/api.py @@ -183,7 +183,7 @@ def fake_time(): def fake_time_ns(): if _should_use_real_time(): return real_time_ns() - return int(int(fake_time()) * 1e9) + return int(fake_time() * 1e9) def fake_localtime(t=None): diff --git a/tests/test_datetimes.py b/tests/test_datetimes.py index 0632f9f..d22c593 100644 --- a/tests/test_datetimes.py +++ b/tests/test_datetimes.py @@ -759,14 +759,25 @@ def test_time_ns(): utc_time = local_time - datetime.timedelta(seconds=time.timezone) expected_timestamp = time.mktime(utc_time.timetuple()) - freezer.start() - assert time.time() == expected_timestamp - assert time.time_ns() == expected_timestamp * 1e9 - freezer.stop() + with freezer: + assert time.time() == expected_timestamp + assert time.time_ns() == expected_timestamp * 1e9 + assert time.time() != expected_timestamp assert time.time_ns() != expected_timestamp * 1e9 +@pytest.mark.skipif(not HAS_TIME_NS, + reason="time.time_ns is present only on 3.7 and above") +def test_time_ns_with_microseconds(): + freezer = freeze_time("2024-03-20 18:21:10.12345") + + with freezer: + assert time.time_ns() == 1710958870123450112 + + assert time.time_ns() != 1710958870123450112 + + def test_compare_datetime_and_time_with_timezone(monkeypatch): """ Compare the result of datetime.datetime.now() and time.time() in a non-UTC timezone. These