2021-10-10 16:25:50 +02:00
|
|
|
defmodule Mobilizon.Service.TimezoneDetector do
|
|
|
|
@moduledoc """
|
|
|
|
Detect the timezone from a point
|
|
|
|
"""
|
|
|
|
|
|
|
|
@type detectable :: Geo.Point.t() | Geo.PointZ.t() | {float() | float()}
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Detect the most appropriate timezone from a value, a geographic set of coordinates and a fallback
|
|
|
|
"""
|
2021-10-12 08:49:12 +02:00
|
|
|
@spec detect(String.t() | nil, detectable() | nil, String.t()) :: String.t()
|
|
|
|
def detect(tz, nil, fallback), do: detect(tz, fallback)
|
|
|
|
|
2021-10-10 16:25:50 +02:00
|
|
|
def detect(nil, geo, fallback) do
|
|
|
|
case TzWorld.timezone_at(geo) do
|
|
|
|
{:ok, timezone} ->
|
|
|
|
timezone
|
|
|
|
|
|
|
|
{:error, :time_zone_not_found} ->
|
|
|
|
fallback
|
2021-11-29 09:31:39 +01:00
|
|
|
|
|
|
|
{:error, :enoent} ->
|
|
|
|
fallback
|
2021-10-10 16:25:50 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def detect(timezone, geo, fallback) do
|
|
|
|
if Tzdata.zone_exists?(timezone) do
|
|
|
|
timezone
|
|
|
|
else
|
|
|
|
detect(nil, geo, fallback)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec detect(String.t() | nil, String.t()) :: String.t()
|
|
|
|
def detect(nil, fallback), do: fallback
|
|
|
|
|
|
|
|
def detect(timezone, fallback) do
|
|
|
|
if Tzdata.zone_exists?(timezone) do
|
|
|
|
timezone
|
|
|
|
else
|
|
|
|
fallback
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|