Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional nanos argument to uuid6 and uuid7 #51

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions python/uuid_utils/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,16 @@ def uuid5(namespace: UUID, name: str) -> UUID:
"""Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
...

def uuid6(node: _Int | None = None, timestamp: _Int | None = None) -> UUID:
def uuid6(
node: _Int | None = None, timestamp: _Int | None = None, nanos: _Int | None = None
) -> UUID:
"""Generate a version 6 UUID using the given timestamp and a host ID.
This is similar to version 1 UUIDs,
except that it is lexicographically sortable by timestamp.
"""
...

def uuid7(timestamp: _Int | None = None) -> UUID:
def uuid7(timestamp: _Int | None = None, nanos: _Int | None = None) -> UUID:
"""Generate a version 7 UUID using a time value and random bytes."""
...

Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ fn uuid5(namespace: &UUID, name: &str) -> PyResult<UUID> {
}

#[pyfunction]
fn uuid6(node: Option<u64>, timestamp: Option<u64>) -> PyResult<UUID> {
fn uuid6(node: Option<u64>, timestamp: Option<u64>, nanos: Option<u32>) -> PyResult<UUID> {
let node = match node {
Some(node) => node.to_ne_bytes(),
None => _getnode().to_ne_bytes(),
Expand All @@ -347,7 +347,7 @@ fn uuid6(node: Option<u64>, timestamp: Option<u64>) -> PyResult<UUID> {

let uuid = match timestamp {
Some(timestamp) => {
let timestamp = Timestamp::from_unix(&Context::new_random(), timestamp, 0);
let timestamp = Timestamp::from_unix(&Context::new_random(), timestamp, nanos.unwrap_or(0));
return Ok(UUID {
uuid: Uuid::new_v6(timestamp, node),
});
Expand All @@ -358,10 +358,10 @@ fn uuid6(node: Option<u64>, timestamp: Option<u64>) -> PyResult<UUID> {
}

#[pyfunction]
fn uuid7(timestamp: Option<u64>) -> PyResult<UUID> {
fn uuid7(timestamp: Option<u64>, nanos: Option<u32>) -> PyResult<UUID> {
let uuid = match timestamp {
Some(timestamp) => {
let timestamp = Timestamp::from_unix(&Context::new_random(), timestamp, 0);
let timestamp = Timestamp::from_unix(&Context::new_random(), timestamp, nanos.unwrap_or(0));
return Ok(UUID {
uuid: Uuid::new_v7(timestamp),
});
Expand Down
13 changes: 13 additions & 0 deletions tests/test_uuid.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import copy
import pickle
import sys
from datetime import datetime
from uuid import UUID, getnode

import pytest
Expand Down Expand Up @@ -92,6 +93,9 @@ def test_uuid6() -> None:
uuid = uuid_utils.uuid6(getnode(), 1679665408)
assert isinstance(uuid, uuid_utils.UUID)

uuid = uuid_utils.uuid6(getnode(), 1679665408, 123)
assert isinstance(uuid, uuid_utils.UUID)

uuid = uuid_utils.uuid6()
assert isinstance(uuid, uuid_utils.UUID)

Expand All @@ -100,9 +104,18 @@ def test_uuid7() -> None:
uuid = uuid_utils.uuid7(1679665408)
assert isinstance(uuid, uuid_utils.UUID)

uuid = uuid_utils.uuid7(1679665408, 999)
assert isinstance(uuid, uuid_utils.UUID)

uuid = uuid_utils.uuid7()
assert isinstance(uuid, uuid_utils.UUID)

ts = datetime(
year=2024, month=1, day=2, hour=3, minute=4, second=5, microsecond=123000
)
uuid = uuid_utils.uuid7(int(ts.timestamp()), ts.microsecond * 1_000)
assert uuid.timestamp == int(ts.timestamp() * 1000)
Copy link
Owner

Choose a reason for hiding this comment

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

Just an FYI, I changed your test to use the timestamp property of the UUID which is an extra property compared to the stdlib.

Copy link
Owner

Choose a reason for hiding this comment

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

Probably a good idea to add documentation to this project 😅



def test_uuid8() -> None:
uuid = uuid_utils.uuid8(b"1234567812345678")
Expand Down
Loading