From 5e1f26f168b4e9949d289b2286f00c55ceedffe3 Mon Sep 17 00:00:00 2001 From: Ben Beasley Date: Tue, 19 Mar 2024 07:19:58 -0400 Subject: [PATCH] Use time.time_ns() to default-initialize (#16) Fix #12. --- ulid/__init__.py | 4 +++- ulid/constants.py | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/ulid/__init__.py b/ulid/__init__.py index 1ff7164..0eaeb33 100644 --- a/ulid/__init__.py +++ b/ulid/__init__.py @@ -70,7 +70,9 @@ class ULID: def __init__(self, value: bytes | None = None) -> None: if value is not None and len(value) != constants.BYTES_LEN: raise ValueError("ULID has to be exactly 16 bytes long.") - self.bytes: bytes = value or ULID.from_timestamp(time.time()).bytes + self.bytes: bytes = ( + value or ULID.from_timestamp(time.time_ns() // constants.NANOSECS_IN_MILLISECS).bytes + ) @classmethod @validate_type(datetime) diff --git a/ulid/constants.py b/ulid/constants.py index a0a1995..bc182c6 100644 --- a/ulid/constants.py +++ b/ulid/constants.py @@ -1,4 +1,5 @@ MILLISECS_IN_SECS = 1000 +NANOSECS_IN_MILLISECS = 1000000 TIMESTAMP_LEN = 6 RANDOMNESS_LEN = 10