Macaw.Draw.Scale.Time (macaw_draw v0.1.0)

Copy Markdown View Source

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.

t()

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.

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.

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

error()

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

A refusal: faults with their paths.

interval()

@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()

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

value()

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

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

zone_error()

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

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

Functions

apply(arg1, value)

@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(scale, list)

@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(arg1)

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

0: a time scale has no bandwidth.

check_zone(zone)

@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(scale, clamp?)

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

Sets clamping; answers the struct.

domain(scale, arg2)

@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!(scale, domain)

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

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

interval(span_seconds, count)

@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(arg1, value)

@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(arg1, value)

@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()

@spec new() :: t()

A time scale with no domain and no zone.

nice(scale)

@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(scale, padding)

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

Identity: a time scale has no padding.

range(scale, range)

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

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

range!(scale, range)

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

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

ticks(scale, count)

@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(scale, zone)

@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!(scale, zone)

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

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