Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/time_machine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ def start(self) -> Traveller:
original_uuid_uuid_create = uuid._UuidCreate # type: ignore[attr-defined]
uuid._generate_time_safe = None # type: ignore[attr-defined]
uuid._UuidCreate = None # type: ignore[attr-defined]
uuid._last_timestamp_v7 = None # type: ignore[attr-defined]

traveller = Traveller(
destination_timestamp=self.destination_timestamp,
Expand Down
26 changes: 26 additions & 0 deletions tests/test_time_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,10 @@ def time_from_uuid1(value: uuid.UUID) -> dt.datetime:
return dt.datetime(1582, 10, 15) + dt.timedelta(microseconds=value.time // 10)


def time_from_uuid7(value: uuid.UUID) -> dt.datetime:
return dt.datetime.fromtimestamp((value.int >> 80) / 1000)


def test_uuid1():
"""
Test that the uuid.uuid1() methods generate values for the destination.
Expand All @@ -917,6 +921,28 @@ def test_uuid1():
assert time_from_uuid1(uuid.uuid1()) == destination


@pytest.mark.skipif(
sys.version_info < (3, 14),
reason="Only valid on Python 3.14+",
)
def test_uuid7_future() -> None:
"""
Test that we can go back in time after setting a future date.
Normally UUID7 would disallow this, since it keeps track of
the _last_timestamp_v7, but we override that now.
"""
if not hasattr(uuid, "uuid7"):
pytest.skip("uuid.uuid7 is not available")
Comment on lines +934 to +935
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only to make mypy happy. What is prefered here?
@pytest.mark.skipif(...) or internal check?


destination_future = dt.datetime(2056, 2, 6, 14, 3, 21)
with time_machine.travel(destination_future, tick=False):
assert time_from_uuid7(uuid.uuid7()) == destination_future

destination_past = dt.datetime(1978, 7, 6, 23, 6, 31)
with time_machine.travel(destination_past, tick=False):
assert time_from_uuid7(uuid.uuid7()) == destination_past


# error handling tests


Expand Down