-
Notifications
You must be signed in to change notification settings - Fork 13
Open
Description
Hi, and thanks for this package!
I'm proposing an update for the uuid6, uuid7 and uuid8 functions, where you could optionally specify the timestamp that gets used when generating the UUID value. For example for the uuid7 function:
def uuid7(timestamp_ms: int = None) -> UUID:
global _last_v7_timestamp
if timestamp_ms is None:
nanoseconds = time.time_ns()
timestamp_ms = nanoseconds // 10**6
# (rest of function)
I haven't checked whether this isn't allowed in the spec, but I feel that this would be really useful in situations where you need to generate UUIDs for historical data, where you have the records' creation timestamp available:
# Django model example
for obj in Model.objects.iterator():
timestamp = calendar.timegm(obj.created_at.utctimetuple())
timestamp_ms = timestamp * 10**3
obj.new_id = uuid7(timestamp_ms)
obj.save()
This would allow for updating a system to start creating new records with the current timestamp, and a data migration for historical data, retaining the sortability by the UUID timestamp part.
Thanks in advance!
eofs, elonzh, dsansot-ru, Bakrog, daniel347x and 1 more
Metadata
Metadata
Assignees
Labels
No labels