···
+
Struct and helper functions for dealing with AT Protocol TIDs (Timestamp
+
Identifiers), a 13-character string representation of a 64-bit number
+
comprised of a Unix timestamp (in microsecond precision) and a random "clock
+
identifier" to help avoid collisions.
+
ATProto spec: https://atproto.com/specs/tid
+
TID strings are always 13 characters long. All bits in the 64-bit number are
+
encoded, essentially meaning that the string is padded with "2" if necessary,
+
(the 0th character in the base32-sortable alphabet).
+
alias Atex.Base32Sortable
+
@re ~r/^[234567abcdefghij][234567abcdefghijklmnopqrstuvwxyz]{12}$/
+
A Unix timestamp representing when the TID was created.
+
@type timestamp() :: integer()
+
An integer to be used for the lower 10 bits of the TID.
+
@type clock_id() :: 0..1023
+
typedstruct enforce: true do
+
field :timestamp, timestamp()
+
field :clock_id, clock_id()
+
Returns a TID for the current moment in time, along with a random clock ID.
+
timestamp: DateTime.utc_now(:microsecond) |> DateTime.to_unix(:microsecond),
+
clock_id: gen_clock_id()
+
Create a new TID from a `DateTime` or an integer representing a Unix timestamp in microseconds.
+
If `clock_id` isn't provided, a random one will be generated.
+
@spec new(DateTime.t() | integer(), integer() | nil) :: t()
+
def new(source, clock_id \\ nil)
+
def new(%DateTime{} = datetime, clock_id),
+
timestamp: DateTime.to_unix(datetime, :microsecond),
+
clock_id: clock_id || gen_clock_id()
+
def new(unix, clock_id) when is_integer(unix),
+
do: %__MODULE__{timestamp: unix, clock_id: clock_id || gen_clock_id()}
+
Convert a TID struct to an instance of `DateTime`.
+
def to_datetime(%__MODULE__{} = tid), do: DateTime.from_unix(tid.timestamp, :microsecond)
+
Generate a random integer to be used as a `clock_id`.
+
@spec gen_clock_id() :: clock_id()
+
def gen_clock_id, do: :rand.uniform(1024) - 1
+
Decode a TID string into an `Atex.TID` struct, returning an error if it's invalid.
+
Syntactically valid TIDs:
+
iex> Atex.TID.decode("3jzfcijpj2z2a")
+
{:ok, %Atex.TID{clock_id: 6, timestamp: 1688137381887007}}
+
iex> Atex.TID.decode("7777777777777")
+
{:ok, %Atex.TID{clock_id: 165, timestamp: 5811096293381285}}
+
iex> Atex.TID.decode("3zzzzzzzzzzzz")
+
{:ok, %Atex.TID{clock_id: 1023, timestamp: 2251799813685247}}
+
iex> Atex.TID.decode("2222222222222")
+
{:ok, %Atex.TID{clock_id: 0, timestamp: 0}}
+
iex> Atex.TID.decode("3jzfcijpj2z21")
+
iex> Atex.TID.decode("0000000000000")
+
iex> Atex.TID.decode("3JZFCIJPJ2Z2A")
+
iex> Atex.TID.decode("3jzfcijpj2z2aa")
+
iex> Atex.TID.decode("3jzfcijpj2z2")
+
iex> Atex.TID.decode("222")
+
# legacy dash syntax *not* supported (TTTT-TTT-TTTT-CC)
+
iex> Atex.TID.decode("3jzf-cij-pj2z-2a")
+
# high bit can't be set
+
iex> Atex.TID.decode("zzzzzzzzzzzzz")
+
iex> Atex.TID.decode("kjzfcijpj2z2a")
+
@spec decode(String.t()) :: {:ok, t()} | :error
+
def decode(<<timestamp::binary-size(11), clock_id::binary-size(2)>> = tid) do
+
if Regex.match?(@re, tid) do
+
timestamp = Base32Sortable.decode(timestamp)
+
clock_id = Base32Sortable.decode(clock_id)
+
def decode(_tid), do: :error
+
Encode a TID struct into a string.
+
iex> Atex.TID.encode(%Atex.TID{clock_id: 6, timestamp: 1688137381887007})
+
iex> Atex.TID.encode(%Atex.TID{clock_id: 165, timestamp: 5811096293381285})
+
iex> Atex.TID.encode(%Atex.TID{clock_id: 1023, timestamp: 2251799813685247})
+
iex> Atex.TID.encode(%Atex.TID{clock_id: 0, timestamp: 0})
+
@spec encode(t()) :: String.t()
+
def encode(%__MODULE__{} = tid) do
+
timestamp = tid.timestamp |> Base32Sortable.encode() |> String.pad_leading(11, "2")
+
clock_id = (tid.clock_id &&& 1023) |> Base32Sortable.encode() |> String.pad_leading(2, "2")
+
defimpl String.Chars, for: Atex.TID do
+
def to_string(tid), do: Atex.TID.encode(tid)