Packages
tz
0.7.0
0.28.2
0.28.1
0.28.0
0.27.3
0.27.2
0.27.1
0.27.0
0.26.6
0.26.5
0.26.4
0.26.3
0.26.2
0.26.1
0.26.0
0.25.1
0.25.0
0.24.0
0.23.0
0.22.0
0.21.1
0.21.0
0.20.1
0.20.0
0.19.0
0.17.0
0.16.2
0.16.1
0.16.0
0.15.0
0.14.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.2
0.8.1
0.8.0
0.7.3
0.7.2
0.7.1
0.7.0
0.6.0
0.5.0
0.4.0
0.3.0
0.2.0
0.1.0
Time zone support for Elixir
Current section
Files
Jump to
Current section
Files
lib/watch_periodically.ex
defmodule Tz.WatchPeriodically do
use GenServer
require Logger
alias Tz.PeriodsProvider
alias Tz.HTTP.HTTPClient
alias Tz.HTTP.HTTPResponse
def start_link(_) do
unless Code.ensure_loaded?(Mint.HTTP) do
raise "Add mint to mix.exs to enable automatic time zone data updates"
end
GenServer.start_link(__MODULE__, %{})
end
def init(state) do
schedule_work()
{:ok, state}
end
def handle_info(:work, state) do
Logger.debug("Tz is checking for IANA time zone database updates")
case fetch_iana_tz_version() do
{:ok, latest_version} ->
if latest_version != PeriodsProvider.version() do
link = "https://data.iana.org/time-zones/releases/tzdata#{latest_version}.tar.gz"
Logger.warn("Tz found a more recent time zone database available for download at #{link}")
end
:error ->
Logger.error("Tz failed to read the latest version of the IANA time zone database")
end
schedule_work()
{:noreply, state}
end
defp schedule_work() do
Process.send_after(self(), :work, 24 * 60 * 60 * 1000) # In 24 hours
end
defp fetch_iana_tz_version() do
case HTTPClient.request("GET", "/time-zones/tzdb/version", hostname: "data.iana.org") do
%HTTPResponse{body: body, status_code: 200} ->
{:ok, body |> List.first() |> String.trim()}
_ ->
:error
end
end
end