Skip to main content

LSB32 for Python

LSB32 converts timezone-aware Python datetime values into compact, sortable Crockford Base32 identifiers through a bundled WebAssembly module.

Seconds remain the default and use the original seven-character format. Milliseconds, microseconds, and nanoseconds use 9, 11, and 13 characters.

Installation

pip install lsb32

The package supports Python 3.9 and newer and depends on the official wasmtime runtime.

Quick Start

from datetime import datetime, timezone
from LSB32 import decode, encode

original = datetime(2026, 1, 1, tzinfo=timezone.utc)
value = encode(original)

assert value == "D110000"
assert decode(value) == original

Optional Precision

Select precision with the Precision enum or its string value. Digits finer than the selected precision are truncated.

from datetime import datetime, timezone
from LSB32 import Precision, decode, encode

original = datetime(2026, 1, 1, 0, 0, 0, 123456, timezone.utc)

assert encode(original, precision=Precision.MILLISECONDS) == "D1100003V"
assert encode(original, precision=Precision.MICROSECONDS) == "D1100003VE8"
assert encode(original, precision=Precision.NANOSECONDS) == "D1100003VE800"

Python datetime stores microseconds. For arbitrary nanoseconds, pass the nanosecond within the second explicitly and decode with decode_precise:

from LSB32 import Precision, decode_precise, encode

value = encode(
    datetime(2026, 1, 1, tzinfo=timezone.utc),
    precision=Precision.NANOSECONDS,
    nanosecond=123456789,
)
restored = decode_precise(value)

assert value == "D1100003VE8RN"
assert restored.nanosecond == 123456789
assert restored.value.microsecond == 123456

decode raises LSB32.Error instead of silently discarding nanoseconds that Python datetime cannot represent.

Custom Epoch

The default epoch covers 2000 through 2063. Pass the same custom epoch to producers and consumers:

original = datetime(2089, 12, 31, 23, 59, 59, tzinfo=timezone.utc)
value = encode(original, epoch=2026)
assert decode(value, epoch=2026) == original

API

API Result
encode(value, epoch=2000, precision="seconds", nanosecond=None) Canonical str
decode(value, epoch=2000) UTC datetime when exactly representable
decode_precise(value, epoch=2000) PreciseDateTime with exact nanoseconds
now(epoch=2000, precision="seconds") Current time at the selected precision

PreciseDateTime.value is the closest exactly representable Python datetime; PreciseDateTime.nanosecond retains the complete value within the second, and PreciseDateTime.precision records the wire precision.

Errors

Error extends ValueError and exposes the WASM status as code. Strict decoding rejects unsupported lengths, lowercase or ambiguous Crockford characters, non-zero prefix padding, invalid calendar fields, and subsecond groups above 999.

Development

From wrappers/python:

python -m pip install -e .
python -m unittest discover -s tests
python -m build
python -m twine check dist/*

Regenerate synchronized WASM and WAT artifacts from the repository root with node wasm.mjs.

License

This package is licensed under the GNU Lesser General Public License, either version 3 or any later version. See LICENSE and COPYING.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lsb32-0.2.0.tar.gz (67.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lsb32-0.2.0-py3-none-any.whl (67.4 kB view details)

Uploaded Python 3

File details

Details for the file lsb32-0.2.0.tar.gz.

File metadata

  • Download URL: lsb32-0.2.0.tar.gz
  • Upload date:
  • Size: 67.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for lsb32-0.2.0.tar.gz
Algorithm Hash digest
SHA256 efd6151e267d5b61b8c6e24365a748775194bb1f92a5d5098dca154f9ba745aa
MD5 e4e222eac2caa4f7250b256e522b4029
BLAKE2b-256 4735fd26512a96a76b3309135269a8300f61cdbb457e65b0f3e2f7526cad476b

See more details on using hashes here.

File details

Details for the file lsb32-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: lsb32-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 67.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for lsb32-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 78df00d9016dd6e52cf94e720410111f178299a8e2323d78f52d545735af49ed
MD5 b473e9d2f4015884c03ba482dec0c01f
BLAKE2b-256 290cbec8ee5d39c7fef84c76ac48f21981c16780519e89c622759349422a31ba

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page