GoodAnalytics.TimeWindow (GoodAnalytics v0.1.1)

Copy Markdown View Source

Time window helpers built around half-open ranges.

All windows use the convention [start_at, end_at), where start_at is inclusive and end_at is exclusive.

Summary

Functions

True when datetime is inside the half-open window [window.start_at, window.end_at).

Fetches and validates the :window option, returning the t() map.

Returns the contiguous prior window with identical duration (in seconds).

Builds a trailing half-open window [start_at, end_at).

Returns the start timestamp of a trailing window ending at end_at.

Types

t()

@type t() :: %{start_at: DateTime.t(), end_at: DateTime.t()}

Functions

contains?(window, datetime)

@spec contains?(t(), DateTime.t()) :: boolean()

True when datetime is inside the half-open window [window.start_at, window.end_at).

fetch!(opts)

@spec fetch!(keyword()) :: t()

Fetches and validates the :window option, returning the t() map.

Raises ArgumentError when :window is missing or is not a %{start_at: DateTime.t(), end_at: DateTime.t()} map.

previous(window)

@spec previous(t()) :: t()

Returns the contiguous prior window with identical duration (in seconds).

trailing(end_at, amount, unit)

@spec trailing(DateTime.t(), non_neg_integer(), :day | :hour | :minute | :second) ::
  t()

Builds a trailing half-open window [start_at, end_at).

trailing_start(end_at, amount, unit)

@spec trailing_start(
  DateTime.t(),
  non_neg_integer(),
  :day | :hour | :minute | :second
) :: DateTime.t()

Returns the start timestamp of a trailing window ending at end_at.