Generate a UUID from a host ID, sequence number, and the current time. If 'node' is not given, getnode() is used to obtain the hardware address. If 'clock_seq' is given, it is used as the sequence number; otherwise a random 14-bit sequence number is chosen.
(node=None, clock_seq=None)
| 724 | _last_timestamp = None |
| 725 | |
| 726 | def uuid1(node=None, clock_seq=None): |
| 727 | """Generate a UUID from a host ID, sequence number, and the current time. |
| 728 | If 'node' is not given, getnode() is used to obtain the hardware |
| 729 | address. If 'clock_seq' is given, it is used as the sequence number; |
| 730 | otherwise a random 14-bit sequence number is chosen.""" |
| 731 | |
| 732 | # When the system provides a version-1 UUID generator, use it (but don't |
| 733 | # use UuidCreate here because its UUIDs don't conform to RFC 4122). |
| 734 | if _generate_time_safe is not None and node is clock_seq is None: |
| 735 | uuid_time, safely_generated = _generate_time_safe() |
| 736 | try: |
| 737 | is_safe = SafeUUID(safely_generated) |
| 738 | except ValueError: |
| 739 | is_safe = SafeUUID.unknown |
| 740 | # The version field is assumed to be handled by _generate_time_safe(). |
| 741 | return UUID(bytes=uuid_time, is_safe=is_safe) |
| 742 | |
| 743 | global _last_timestamp |
| 744 | nanoseconds = time.time_ns() |
| 745 | # 0x01b21dd213814000 is the number of 100-ns intervals between the |
| 746 | # UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00. |
| 747 | timestamp = nanoseconds // 100 + 0x01b21dd213814000 |
| 748 | if _last_timestamp is not None and timestamp <= _last_timestamp: |
| 749 | timestamp = _last_timestamp + 1 |
| 750 | _last_timestamp = timestamp |
| 751 | if clock_seq is None: |
| 752 | import random |
| 753 | clock_seq = random.getrandbits(14) # instead of stable storage |
| 754 | time_low = timestamp & 0xffffffff |
| 755 | time_mid = (timestamp >> 32) & 0xffff |
| 756 | time_hi_version = (timestamp >> 48) & 0x0fff |
| 757 | clock_seq_low = clock_seq & 0xff |
| 758 | clock_seq_hi_variant = (clock_seq >> 8) & 0x3f |
| 759 | if node is None: |
| 760 | node = getnode() |
| 761 | return UUID(fields=(time_low, time_mid, time_hi_version, |
| 762 | clock_seq_hi_variant, clock_seq_low, node), version=1) |
| 763 | |
| 764 | def uuid3(namespace, name): |
| 765 | """Generate a UUID from the MD5 hash of a namespace UUID and a name.""" |
no test coverage detected
searching dependent graphs…