Time and IDs

KSUID

KSUIDs, Segment's K-Sortable Unique IDs: 27 base62 characters sorted by the second they were made, with 128 random bits; made, read back and checked.

Version 1.0.0 · 4 functions · 1 crate

#ids#time#api#database

Functions

What use ksuid.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use ksuid, ksuid_at from ksuid (Quillo 0.6).

ksuid() → str

A new KSUID, such as "2W1wBpjVQS0EOmrbRBvyGrKkZ3Y": base62, safe in URLs, later ones sorting after earlier ones (to the second).

ksuid_at(time_ms: int) → stror an error

A KSUID for a time in milliseconds since 1970 (as now() gives; KSUIDs keep whole seconds, from 2014-05-13 on), the rest random: for old rows, or the first id of a time range.

ksuid_parse(id: str) → anyor an error

What a KSUID holds: { time_ms, payload }, time_ms when it was made (to the second) in milliseconds since 1970, payload its 16 random bytes in hex. An error when it is not a KSUID.

ksuid_valid(id: str) → bool

Whether id is a KSUID: 27 base62 characters within the format's range.

Get it

$ quillo install ksuid               # ksuid.qwer into this folder$ quillo install ksuid --example     # and its example, ksuid.qwe

Or with curl:

$ curl -O https://quillolang.org/plugins/files/ksuid.qwer$ curl -O https://quillolang.org/plugins/files/ksuid.qwe

Then, in your program, all of it or only what you call:

main.qwe
use ksuid.qweruse ksuid, ksuid_at from ksuid      // Quillo 0.6: only these

It uses this crate, which cargo fetches the first time the plugin is built:

[dependencies]
svix-ksuid = "0.10"

Example

ksuid.qwe
// Event ids for a webhook log: sortable by time, unique without a database sequence.////   quillo run ksuid.qwe      then open http://localhost:8080/eventsuse "ksuid.qwer"route GET /events {    let old = ksuid_at(1767225600000)?         // 2026-01-01    let fresh = ksuid()    { fresh, old, sorted: sort([fresh, old]) == [old, fresh], old_time: ksuid_parse(old)?.time_ms, valid: [ksuid_valid(fresh), ksuid_valid("nope")] }}route GET /read ? { id: str } = ksuid_parse(id)?
$ quillo run ksuid.qwe$ curl 'localhost:8080/events'$ curl 'localhost:8080/read?id=0ujtsYcgvSTl8PAuAdqWYSMnLOv'

Source

ksuid.qwer
//! KSUIDs (K-Sortable Unique IDs, Segment's format): 27 characters, sorted by the second they were made, with 128 random bits so they never clash; made, read and checked.//! version: 1.0.0//!//! [dependencies]//! svix-ksuid = "0.10"use serde_json::{json, Value};use std::str::FromStr;use svix_ksuid::{Ksuid, KsuidLike};/// A new KSUID, such as "2W1wBpjVQS0EOmrbRBvyGrKkZ3Y": base62, safe in URLs, later ones sorting after earlier ones (to the second).pub fn ksuid() -> String {    let secs = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_secs() as i64);    Ksuid::from_seconds(Some(secs), None).to_string()}/// A KSUID for a time in milliseconds since 1970 (as `now()` gives; KSUIDs keep whole seconds, from 2014-05-13 on), the rest random: for old rows, or the first id of a time range.pub fn ksuid_at(time_ms: i64) -> Result<String, String> {    let secs = time_ms.div_euclid(1000);    if !(1_400_000_000..1_400_000_000 + u32::MAX as i64).contains(&secs) {        return Err("KSUID times run from 2014-05-13 to 2150".into());    }    Ok(Ksuid::from_seconds(Some(secs), None).to_string())}/// What a KSUID holds: `{ time_ms, payload }`, `time_ms` when it was made (to the second) in milliseconds since 1970, `payload` its 16 random bytes in hex. An error when it is not a KSUID.pub fn ksuid_parse(id: &str) -> Result<Value, String> {    let k = Ksuid::from_str(id.trim()).map_err(|e| format!("{:?} is not a KSUID: {}", id, e))?;    let hex: String = k.payload().iter().map(|b| format!("{:02x}", b)).collect();    Ok(json!({ "time_ms": k.timestamp_seconds() * 1000, "payload": hex }))}/// Whether `id` is a KSUID: 27 base62 characters within the format's range.pub fn ksuid_valid(id: &str) -> bool {    let t = id.trim();    t.len() == 27 && Ksuid::from_str(t).is_ok()}
Thread
87Useful? Like it, or add to its thread below.

Thread 0 posts

On KSUID: questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.