Skip to content

Commit

Permalink
Use time.time_ns() to default-initialize (#16)
Browse files Browse the repository at this point in the history
Fix #12.
  • Loading branch information
musicinmybrain authored Mar 19, 2024
1 parent 341a7a0 commit 5e1f26f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ulid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions ulid/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
MILLISECS_IN_SECS = 1000
NANOSECS_IN_MILLISECS = 1000000

TIMESTAMP_LEN = 6
RANDOMNESS_LEN = 10
Expand Down

0 comments on commit 5e1f26f

Please sign in to comment.