# `Macaw.Draw.Scale.Time`
[🔗](https://github.com/dcoai/macaw_draw/blob/v0.1.0/lib/macaw/draw/scale/time.ex#L1)

A continuous scale over instants, with calendar ticks and a display zone (spec/06 §8).

**Values.** A domain bound or an `apply/2` argument is a `DateTime`, a `NaiveDateTime`
(read as UTC), a `Date` (midnight UTC) or an integer, microseconds since the Unix epoch,
which is what a packed time column holds. Each reduces to Unix microseconds and no
precision is discarded: two readings 400 ms apart map to distinct positions. Any other
value applies to `nil`. `apply_instants/2` maps a column of integer instants in one call,
the domain's ends reduced once.

**Ticks** follow d3-time: an interval (seconds, minutes, hours, days, weeks, months,
years or a multiple of years, `interval/2`) is chosen so that about `count` ticks fit the
domain, and every tick sits on a boundary of that interval inside the domain.

**The display zone** (`zone/2`) is an IANA name, looked up in the host's
`Calendar.TimeZoneDatabase`. It changes where ticks fall and how values show, never where
a value is drawn: boundaries are read in the zone's local calendar, and `ticks/2`,
`nice/1` and `invert/2` answer `DateTime` values in the zone, UTC without one. An unknown
zone is refused as a value, never a fallback to UTC.

# `error`

```elixir
@type error() :: {:error, [Macaw.Base.Fault.t()]}
```

A refusal: faults with their paths.

# `interval`

```elixir
@type interval() ::
  {:second | :minute | :hour | :day | :week | :month | :year, pos_integer()}
```

A tick interval: a unit and how many of it make one step.

# `t`

```elixir
@type t() :: %Macaw.Draw.Scale.Time{
  clamp?: boolean(),
  domain: [value()] | nil,
  range: [number()],
  zone: Calendar.time_zone() | nil
}
```

# `value`

```elixir
@type value() :: DateTime.t() | NaiveDateTime.t() | Date.t() | integer()
```

A time value the scale reads (spec/06 §8.2).

# `zone_error`

```elixir
@type zone_error() :: :time_zone_not_found | :utc_only_time_zone_database
```

Why the host's time zone database cannot show a zone.

# `apply`

```elixir
@spec apply(t(), term()) :: float() | nil | error()
```

The position of a time value: linear interpolation over Unix microseconds, clamping `t`
when the scale clamps; `nil` for a value that is no time value or a scale with no
domain. An integer is microseconds since the epoch, and builds no struct.

# `apply_instants`

```elixir
@spec apply_instants(t(), [integer() | nil]) :: [float() | nil] | error()
```

A column of integer microseconds mapped in one call, `nil` where a reading is missing:
each element exactly `apply/2` of it, the domain's ends reduced once for the list
(spec/06 §8.2).

# `bandwidth`

```elixir
@spec bandwidth(t()) :: 0 | error()
```

`0`: a time scale has no bandwidth.

# `check_zone`

```elixir
@spec check_zone(term()) :: :ok | {:error, zone_error()}
```

Whether the host's time zone database can show `zone`: `:ok`, or
`{:error, :time_zone_not_found}` or `{:error, :utc_only_time_zone_database}` (a host
with no database configured). A name that is no string is not found.

# `clamp`

```elixir
@spec clamp(t(), boolean()) :: t() | error()
```

Sets clamping; answers the struct.

# `domain`

```elixir
@spec domain(t(), term()) :: {:ok, t()} | error()
```

Sets the domain, two time values (spec/06 §8.2): `{:ok, scale}`, or `{:error, faults}`
with `{[i], {:type, :time}}` for an element of another kind.

# `domain!`

```elixir
@spec domain!(t(), term()) :: t()
```

`domain/2`, answering the scale or raising `Macaw.Draw.Error`.

# `interval`

```elixir
@spec interval(number(), number()) :: interval() | error()
```

The interval about `count` ticks use over a span of `span_seconds` (spec/06 §8.3.1): of
the two table rows that bracket `span_seconds / count`, the one closer in ratio;
`{:second, 1}` below a second; above a year, `{:year, k}` with `k` the tick rule's step
over years, `Macaw.Draw.Ticks.step(0, span_years, count)`. Not part of the protocol: for
a caller that formats ticks by their interval.

    iex> Macaw.Draw.Scale.Time.interval(24 * 31_536_000, 10)
    {:year, 2}

# `invert`

```elixir
@spec invert(t(), term()) :: DateTime.t() | nil | error()
```

The instant of a range value, a `DateTime` in the scale's zone (UTC without one) at
microsecond precision; never clamps. `nil` for a value that is no number, a scale with
no domain, or an instant past the years a `DateTime` holds.

# `local`

```elixir
@spec local(t(), term()) :: term() | error()
```

A value as the scale shows it: a time value reduced as `apply/2` reduces it and shifted
to the scale's zone, or the value itself when the scale has none or it is no time value.

# `new`

```elixir
@spec new() :: t()
```

A time scale with no domain and no zone.

# `nice`

```elixir
@spec nice(t()) :: t() | error()
```

The domain extended outward to the boundaries of the interval ten ticks would use, in
its own orientation, as `DateTime` values in the scale's zone. A domain already on
boundaries is unchanged; a scale with no domain is answered as it is.

# `padding`

```elixir
@spec padding(t(), number()) :: t() | error()
```

Identity: a time scale has no padding.

# `range`

```elixir
@spec range(t(), term()) :: {:ok, t()} | error()
```

Sets the range, two numbers: `{:ok, scale}` or `{:error, faults}`.

# `range!`

```elixir
@spec range!(t(), term()) :: t()
```

`range/2`, answering the scale or raising `Macaw.Draw.Error`.

# `ticks`

```elixir
@spec ticks(t(), number()) :: [DateTime.t()] | error()
```

The boundaries of the chosen interval inside the domain, as `DateTime` values in the
scale's zone (UTC without one), read in that zone's local calendar (spec/06 §8.3, §8.4).
A descending domain gives them descending; a scale with no domain has none. A zone the
host's database no longer shows is its fault.

# `zone`

```elixir
@spec zone(t(), term()) :: {:ok, t()} | error()
```

Sets the display zone: an IANA name such as `"America/New_York"`, or `nil` for UTC
(spec/06 §8.4). `{:ok, scale}`, or `{:error, [{[], {:zone, name, reason}}]}` when the
host's `Calendar.TimeZoneDatabase` does not know the name or the host has none: never a
silent fallback to UTC.

# `zone!`

```elixir
@spec zone!(t(), term()) :: t()
```

`zone/2`, answering the scale or raising `Macaw.Draw.Error`.

---

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