- Delete dead config keys and test file (typo'd aprsme_is_*, vacuous disable test) - Remove duplicate function clauses and identical function pairs - Rename misleading one_hour_ago variable to one_day_ago - Strip stale Oban comment - Remove TestHelpers time function duplicates - Inline Aprsme.Schema module (only 1 caller) - Deduplicate prod esbuild config - Thin SymbolRenderer delegations, inline TimeUtils wrappers - Remove redundant DeploymentNotifier GenServer polling - Replace random fallback in get_callsign_key with sentinel
38 lines
1.3 KiB
Elixir
38 lines
1.3 KiB
Elixir
defmodule AprsmeWeb.TimeUtils do
|
|
@moduledoc """
|
|
Common time calculation utilities to reduce duplication across the codebase.
|
|
"""
|
|
|
|
@doc """
|
|
Returns a DateTime that is the specified number of hours before now.
|
|
"""
|
|
@spec hours_ago(number()) :: DateTime.t()
|
|
def hours_ago(hours) when is_number(hours) do
|
|
DateTime.add(DateTime.utc_now(), -hours * 3600, :second)
|
|
end
|
|
|
|
@doc """
|
|
Returns a DateTime that is the specified number of days before now.
|
|
"""
|
|
@spec days_ago(number()) :: DateTime.t()
|
|
def days_ago(days) when is_number(days) do
|
|
DateTime.add(DateTime.utc_now(), -days, :day)
|
|
end
|
|
|
|
@type time_range_atom :: :last_hour | :last_day | :last_two_days | :last_week
|
|
|
|
@doc """
|
|
Returns a tuple of {start_time, end_time} for common time ranges.
|
|
"""
|
|
@spec time_range(time_range_atom()) :: {DateTime.t(), DateTime.t()}
|
|
def time_range(:last_hour), do: {hours_ago(1), DateTime.utc_now()}
|
|
def time_range(:last_day), do: {hours_ago(24), DateTime.utc_now()}
|
|
def time_range(:last_two_days), do: {hours_ago(48), DateTime.utc_now()}
|
|
def time_range(:last_week), do: {hours_ago(24 * 7), DateTime.utc_now()}
|
|
|
|
@doc """
|
|
Returns a default time range of 48 hours.
|
|
"""
|
|
@spec default_time_range() :: {DateTime.t(), DateTime.t()}
|
|
def default_time_range, do: time_range(:last_two_days)
|
|
end
|