Similar to :func:`uuid1` but where fields are ordered differently for improved DB locality. More precisely, given a 60-bit timestamp value as specified for UUIDv1, for UUIDv6 the first 48 most significant bits are stored first, followed by the 4-bit version (same position), followed
(node=None, clock_seq=None)
| 794 | _last_timestamp_v6 = None |
| 795 | |
| 796 | def uuid6(node=None, clock_seq=None): |
| 797 | """Similar to :func:`uuid1` but where fields are ordered differently |
| 798 | for improved DB locality. |
| 799 | |
| 800 | More precisely, given a 60-bit timestamp value as specified for UUIDv1, |
| 801 | for UUIDv6 the first 48 most significant bits are stored first, followed |
| 802 | by the 4-bit version (same position), followed by the remaining 12 bits |
| 803 | of the original 60-bit timestamp. |
| 804 | """ |
| 805 | global _last_timestamp_v6 |
| 806 | import time |
| 807 | nanoseconds = time.time_ns() |
| 808 | # 0x01b21dd213814000 is the number of 100-ns intervals between the |
| 809 | # UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00. |
| 810 | timestamp = nanoseconds // 100 + 0x01b21dd213814000 |
| 811 | if _last_timestamp_v6 is not None and timestamp <= _last_timestamp_v6: |
| 812 | timestamp = _last_timestamp_v6 + 1 |
| 813 | _last_timestamp_v6 = timestamp |
| 814 | if clock_seq is None: |
| 815 | import random |
| 816 | clock_seq = random.getrandbits(14) # instead of stable storage |
| 817 | time_hi_and_mid = (timestamp >> 12) & 0xffff_ffff_ffff |
| 818 | time_lo = timestamp & 0x0fff # keep 12 bits and clear version bits |
| 819 | clock_s = clock_seq & 0x3fff # keep 14 bits and clear variant bits |
| 820 | if node is None: |
| 821 | node = getnode() |
| 822 | # --- 32 + 16 --- -- 4 -- -- 12 -- -- 2 -- -- 14 --- 48 |
| 823 | # time_hi_and_mid | version | time_lo | variant | clock_seq | node |
| 824 | int_uuid_6 = time_hi_and_mid << 80 |
| 825 | int_uuid_6 |= time_lo << 64 |
| 826 | int_uuid_6 |= clock_s << 48 |
| 827 | int_uuid_6 |= node & 0xffff_ffff_ffff |
| 828 | # by construction, the variant and version bits are already cleared |
| 829 | int_uuid_6 |= _RFC_4122_VERSION_6_FLAGS |
| 830 | return UUID._from_int(int_uuid_6) |
| 831 | |
| 832 | |
| 833 | _last_timestamp_v7 = None |
nothing calls this directly
no test coverage detected
searching dependent graphs…