Latch.TID (latch v0.8.0)

Copy Markdown

Timestamp identifiers, frequently used as record keys in atproto. Time-sortable, can be used as logical clocks within a system, and designed to reduce the risk of collisions.

https://atproto.com/specs/tid

Summary

Functions

Generate a TID that encodes the given time and a random clock ID, making it unlikely (but not impossible) to result in collisions.

Takes a raw integer and formats it as a TID.

Generate a TID that encodes the current time and a random clock ID, making it unlikely (but not impossible) to result in collisions.

Each TID encodes a timestamp and a 10 bit clock ID. This returns the timestamp part for you as a DateTime. Note that TIDs can be created using any date, and it does not necessarily match when it was created or have any relationship with the data the TID is used for.

Like to_datetime/1 but raises if the datetime is invalid.

Each TID encodes a timestamp and a 10 bit clock ID. This returns the timestamp part for you in the unix microseconds format. Note that TIDs can be created using any date, and it does not necessarily match when it was created or have any relationship with the data the TID is used for.

Validates a given string against a regex to ensure it conforms with the spec.

Functions

at_time(datetime, clock_id \\ random_clock_id())

@spec at_time(DateTime.t(), integer()) :: String.t()

Generate a TID that encodes the given time and a random clock ID, making it unlikely (but not impossible) to result in collisions.

Overriding :clock_id allows you to create deterministic tests.

new(int)

@spec new(non_neg_integer()) :: String.t()

Takes a raw integer and formats it as a TID.

In most cases you'll want to use the helpers now and at_time.

now(opts \\ [])

@spec now(keyword()) :: String.t()

Generate a TID that encodes the current time and a random clock ID, making it unlikely (but not impossible) to result in collisions.

Options

  • :clock_id - override the random clock ID
  • :time_fun - a function that returns the current time as microseconds

Overriding both :clock_id and :time_fun allows you to create deterministic tests.

to_datetime(tid)

Each TID encodes a timestamp and a 10 bit clock ID. This returns the timestamp part for you as a DateTime. Note that TIDs can be created using any date, and it does not necessarily match when it was created or have any relationship with the data the TID is used for.

tid must be a valid tid, use valid?/1 to validate first.

Example:

Latch.TID.to_datetime("3mup4bbh67n2g")
iex> {:ok, ~U[2026-09-04 13:50:51.404467Z]}

to_datetime!(tid)

Like to_datetime/1 but raises if the datetime is invalid.

to_unix(tid)

Each TID encodes a timestamp and a 10 bit clock ID. This returns the timestamp part for you in the unix microseconds format. Note that TIDs can be created using any date, and it does not necessarily match when it was created or have any relationship with the data the TID is used for.

tid must be a valid tid, use valid?/1 to validate first.

valid?(tid)

@spec valid?(String.t()) :: boolean()

Validates a given string against a regex to ensure it conforms with the spec.