# `Latch.TID`

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

# `at_time`

```elixir
@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`

```elixir
@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`

```elixir
@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`

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!`

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

# `to_unix`

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?`

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

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
