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.
Summary
Types
A refusal: faults with their paths.
A tick interval: a unit and how many of it make one step.
A time value the scale reads (spec/06 §8.2).
Why the host's time zone database cannot show a zone.
Functions
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.
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).
0: a time scale has no bandwidth.
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.
Sets clamping; answers the struct.
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/2, answering the scale or raising Macaw.Draw.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.
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.
A time scale with no domain and no zone.
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.
Identity: a time scale has no padding.
Sets the range, two numbers: {:ok, scale} or {:error, faults}.
range/2, answering the scale or raising Macaw.Draw.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.
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/2, answering the scale or raising Macaw.Draw.Error.
Types
@type error() :: {:error, [Macaw.Base.Fault.t()]}
A refusal: faults with their paths.
@type interval() :: {:second | :minute | :hour | :day | :week | :month | :year, pos_integer()}
A tick interval: a unit and how many of it make one step.
@type t() :: %Macaw.Draw.Scale.Time{ clamp?: boolean(), domain: [value()] | nil, range: [number()], zone: Calendar.time_zone() | nil }
@type value() :: DateTime.t() | NaiveDateTime.t() | Date.t() | integer()
A time value the scale reads (spec/06 §8.2).
@type zone_error() :: :time_zone_not_found | :utc_only_time_zone_database
Why the host's time zone database cannot show a zone.
Functions
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.
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).
0: a time scale has no bandwidth.
@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.
Sets clamping; answers the struct.
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/2, answering the scale or raising Macaw.Draw.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}
@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.
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.
@spec new() :: t()
A time scale with no domain and no zone.
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.
Identity: a time scale has no padding.
Sets the range, two numbers: {:ok, scale} or {:error, faults}.
range/2, answering the scale or raising Macaw.Draw.Error.
@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.
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/2, answering the scale or raising Macaw.Draw.Error.