Latch.TID (latch v0.5.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.

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.

valid?(tid)

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

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