Comments (3)
Python code example
# Enough to represent nanoseconds from time.time_ns()
SUBSEC_BITS = 30
SUBSEC_DECIMAL_DIGITS = 9
def subsec_encode(
value: int,
subsec_bits: int = SUBSEC_BITS,
subsec_decimal_digits: int = SUBSEC_DECIMAL_DIGITS,
) -> int:
return value * 2 ** subsec_bits // 10 ** subsec_decimal_digits
def subsec_decode(
value: int,
subsec_bits: int = SUBSEC_BITS,
subsec_decimal_digits: int = SUBSEC_DECIMAL_DIGITS,
) -> int:
return -(-value * 10 ** subsec_decimal_digits // 2 ** subsec_bits)
def test_millisecond():
print("Testing millisecond conversions.")
for i in range(10 ** 3):
if i % 10 ** 2 == 0:
print(f"{i=} ...")
assert i == subsec_decode(subsec_encode(i, 10, 3), 10, 3)
def test_microsecond():
print("Testing microsecond conversions.")
for i in range(10 ** 6):
if i % 10 ** 5 == 0:
print(f"{i=} ...")
assert i == subsec_decode(subsec_encode(i, 20, 6), 20, 6)
def test_nanosecond():
print("Testing nanosecond conversions.")
for i in range(10 ** 9):
if i % 10 ** 6 == 0:
print(f"{i=} ...")
assert i == subsec_decode(subsec_encode(i, 30, 9), 30, 9)
def main():
import time
timestamp = time.time_ns()
unixts, ns = divmod(timestamp, 10 ** SUBSEC_DECIMAL_DIGITS)
subsec = subsec_encode(ns)
subsec_to_ns = subsec_decode(subsec)
print(f"{timestamp=}")
print(f"{unixts=}")
print(f"{ns=}")
print(f"{subsec=}")
print(f"{subsec_to_ns=}")
test_millisecond()
test_microsecond()
test_nanosecond()
if __name__ == "__main__":
main()
Ouput is something like
timestamp=1641075060289465000
unixts=1641075060
ns=289465000
subsec=310810677
subsec_to_ns=289465000
Testing millisecond conversions.
i=0 ...
Those main()
and test_...
functions here are to show that it actually works and maybe you would just include subsec_encode()
and subsec_decode()
in the actual documentation. The encoding and decoding should work correctly even if you change SUBSEC_BITS
to something like the above mentioned 12 for a millisecond precision.
from uuid6-ietf-draft.
I think it might be a good idea to include code examples, at least in a few of the most common programming languages, how to encode and decode those timestamps. Basically every programming language I know expresses precision timestamps as integers. For example https://docs.python.org/3/library/time.html#time.time_ns
from uuid6-ietf-draft.
Great code example @oittaa !
from uuid6-ietf-draft.
Related Issues (20)
- The conditions for the use of source-generated UUIDs and receiver-generated UUIDs HOT 3
- Provide a complete UUIDv8 example
- Encouraging the use of UUID as the primary key HOT 8
- Fork safety HOT 1
- Fix RFC4086 link in Normative References
- Draft 04: MUST to SHOULD to reduce "absolute monotonicity" requirements
- Tout est Terrible. Endian problems with original RFC 4122 + case problems HOT 8
- Draft 05: B.2. Example of a UUIDv7 Value two "var" in table
- Draft 05: MUST veribage in Reliability of 6.1
- Announcement: Post-IETF 114 and the future of this Draft HOT 26
- Typo in UUIDv7 example value HOT 1
- Remove "time-based" constraint from version 8 UUID HOT 1
- Further clarify v7 field description HOT 3
- Required UUIDv7 generator features for RDBMS (PostgreSQL etc.)
- Approximate UUID timestamp calculations HOT 24
- Performance testing for UUIDv7 HOT 14
- RDBMS and other platforms that support UUIDv7
- UUIDv7 logo HOT 20
- Typo in Approximate UUID timestamp calculations HOT 13
- Reserving a special form within UUIDv10 (alternating UUID) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from uuid6-ietf-draft.