Skip to content

Commit a82adff

Browse files
committed
Remove warnings if optional dependency mint is not found
1 parent 43d1c3a commit a82adff

File tree

5 files changed

+146
-148
lines changed

5 files changed

+146
-148
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module can, by default, only operate on datetimes in the UTC time zone. Alternat
88
third-party libraries, such as `tz`, to bring in time zone support and deal with datetimes in other time zones than UTC.
99

1010
The `tz` library relies on the [time zone database](https://data.iana.org/time-zones/tzdb/) maintained by
11-
[IANA](https://www.iana.org). As of version 0.7.0, `tz` uses version _tzdata2019c_ of the IANA time zone database.
11+
[IANA](https://www.iana.org). As of version 0.7.1, `tz` uses version _tzdata2019c_ of the IANA time zone database.
1212

1313
## Features
1414

@@ -50,7 +50,7 @@ defp deps do
5050
[
5151
{:castore, "~> 0.1.5"},
5252
{:mint, "~> 1.0"},
53-
{:tz, "~> 0.7.0"}
53+
{:tz, "~> 0.7.1"}
5454
]
5555
end
5656
```
@@ -103,7 +103,7 @@ Add `tz` for Elixir as a dependency in your `mix.exs` file:
103103
```elixir
104104
def deps do
105105
[
106-
{:tz, "~> 0.7.0"}
106+
{:tz, "~> 0.7.1"}
107107
]
108108
end
109109
```

lib/http/http_client.ex

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
1-
defmodule Tz.HTTP.HTTPClient do
2-
alias Mint.HTTP
3-
alias Tz.HTTP.HTTPResponse
1+
if Code.ensure_loaded?(Mint.HTTP) do
2+
defmodule Tz.HTTP.HTTPClient do
3+
alias Mint.HTTP
4+
alias Tz.HTTP.HTTPResponse
45

5-
def request(method, path, opts) do
6-
hostname = Keyword.fetch!(opts, :hostname)
7-
headers = Keyword.get(opts, :headers, [])
6+
def request(method, path, opts) do
7+
hostname = Keyword.fetch!(opts, :hostname)
8+
headers = Keyword.get(opts, :headers, [])
89

9-
{:ok, conn} = HTTP.connect(:https, hostname, 443, opts)
10-
{:ok, conn, _} = HTTP.request(conn, method, path, headers, nil)
10+
{:ok, conn} = HTTP.connect(:https, hostname, 443, opts)
11+
{:ok, conn, _} = HTTP.request(conn, method, path, headers, nil)
1112

12-
{:ok, response = %HTTPResponse{}} = recv_response(conn)
13-
{:ok, _conn} = HTTP.close(conn)
13+
{:ok, response = %HTTPResponse{}} = recv_response(conn)
14+
{:ok, _conn} = HTTP.close(conn)
1415

15-
response
16-
end
16+
response
17+
end
1718

18-
defp recv_response(conn, http_response \\ %HTTPResponse{}) do
19-
receive do
20-
message ->
21-
{:ok, conn, mint_messages} = HTTP.stream(conn, message)
19+
defp recv_response(conn, http_response \\ %HTTPResponse{}) do
20+
receive do
21+
message ->
22+
{:ok, conn, mint_messages} = HTTP.stream(conn, message)
2223

23-
case HTTPResponse.parse(mint_messages, http_response) do
24-
{:ok, http_response = %HTTPResponse{complete?: true}} ->
25-
{:ok, http_response}
24+
case HTTPResponse.parse(mint_messages, http_response) do
25+
{:ok, http_response = %HTTPResponse{complete?: true}} ->
26+
{:ok, http_response}
2627

27-
{:ok, http_response} ->
28-
recv_response(conn, http_response)
28+
{:ok, http_response} ->
29+
recv_response(conn, http_response)
2930

30-
error ->
31-
error
32-
end
31+
error ->
32+
error
33+
end
34+
end
3335
end
3436
end
3537
end

lib/update_periodically.ex

Lines changed: 78 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,101 @@
1-
defmodule Tz.UpdatePeriodically do
2-
use GenServer
1+
if Code.ensure_loaded?(Mint.HTTP) do
2+
defmodule Tz.UpdatePeriodically do
3+
use GenServer
34

4-
require Logger
5+
require Logger
56

6-
alias Tz.Compiler
7-
alias Tz.PeriodsProvider
8-
alias Tz.HTTP.HTTPClient
9-
alias Tz.HTTP.HTTPResponse
7+
alias Tz.Compiler
8+
alias Tz.PeriodsProvider
9+
alias Tz.HTTP.HTTPClient
10+
alias Tz.HTTP.HTTPResponse
1011

11-
def start_link(_) do
12-
unless Code.ensure_loaded?(Mint.HTTP) do
13-
raise "Add mint to mix.exs to enable automatic time zone data updates"
12+
def start_link(_) do
13+
GenServer.start_link(__MODULE__, %{})
1414
end
1515

16-
GenServer.start_link(__MODULE__, %{})
17-
end
16+
def init(state) do
17+
schedule_work()
18+
{:ok, state}
19+
end
1820

19-
def init(state) do
20-
schedule_work()
21-
{:ok, state}
22-
end
21+
def handle_info(:work, state) do
22+
Logger.debug("Tz is checking for IANA time zone database updates")
2323

24-
def handle_info(:work, state) do
25-
Logger.debug("Tz is checking for IANA time zone database updates")
24+
if maybe_update_tz_database() == :updated do
25+
Logger.info("Tz is recompiling time zone periods...")
26+
Code.compiler_options(ignore_module_conflict: true)
27+
Compiler.compile()
28+
Code.compiler_options(ignore_module_conflict: false)
29+
Logger.info("Tz compilation done")
30+
end
2631

27-
if maybe_update_tz_database() == :updated do
28-
Logger.info("Tz is recompiling time zone periods...")
29-
Code.compiler_options(ignore_module_conflict: true)
30-
Compiler.compile()
31-
Code.compiler_options(ignore_module_conflict: false)
32-
Logger.info("Tz compilation done")
32+
schedule_work()
33+
{:noreply, state}
3334
end
3435

35-
schedule_work()
36-
{:noreply, state}
37-
end
38-
39-
defp schedule_work() do
40-
Process.send_after(self(), :work, 24 * 60 * 60 * 1000) # In 24 hours
41-
end
36+
defp schedule_work() do
37+
Process.send_after(self(), :work, 24 * 60 * 60 * 1000) # In 24 hours
38+
end
4239

43-
defp maybe_update_tz_database() do
44-
case fetch_iana_tz_version() do
45-
{:ok, latest_version} ->
46-
if latest_version != PeriodsProvider.version() do
47-
case update_tz_database(latest_version) do
48-
:ok ->
49-
delete_tz_database(PeriodsProvider.version())
50-
:updated
51-
_ -> :error
40+
defp maybe_update_tz_database() do
41+
case fetch_iana_tz_version() do
42+
{:ok, latest_version} ->
43+
if latest_version != PeriodsProvider.version() do
44+
case update_tz_database(latest_version) do
45+
:ok ->
46+
delete_tz_database(PeriodsProvider.version())
47+
:updated
48+
_ -> :error
49+
end
5250
end
53-
end
54-
:error ->
55-
Logger.error("Tz failed to read the latest version of the IANA time zone database")
56-
:no_update
51+
:error ->
52+
Logger.error("Tz failed to read the latest version of the IANA time zone database")
53+
:no_update
54+
end
5755
end
58-
end
5956

60-
defp fetch_iana_tz_version() do
61-
case HTTPClient.request("GET", "/time-zones/tzdb/version", hostname: "data.iana.org") do
62-
%HTTPResponse{body: body, status_code: 200} ->
63-
{:ok, body |> List.first() |> String.trim()}
64-
_ ->
65-
:error
57+
defp fetch_iana_tz_version() do
58+
case HTTPClient.request("GET", "/time-zones/tzdb/version", hostname: "data.iana.org") do
59+
%HTTPResponse{body: body, status_code: 200} ->
60+
{:ok, body |> List.first() |> String.trim()}
61+
_ ->
62+
:error
63+
end
6664
end
67-
end
6865

69-
defp update_tz_database(version) do
70-
case download_tz_database(version) do
71-
{:ok, content} ->
72-
extract_tz_database(version, content)
73-
:ok
74-
:error ->
75-
Logger.error("Tz failed to download the latest archived IANA time zone database (version #{version})")
76-
:error
66+
defp update_tz_database(version) do
67+
case download_tz_database(version) do
68+
{:ok, content} ->
69+
extract_tz_database(version, content)
70+
:ok
71+
:error ->
72+
Logger.error("Tz failed to download the latest archived IANA time zone database (version #{version})")
73+
:error
74+
end
7775
end
78-
end
7976

80-
defp download_tz_database(version) do
81-
Logger.info("Tz is downloading the latest IANA time zone database (version #{version})...")
82-
case HTTPClient.request("GET", "/time-zones/releases/tzdata#{version}.tar.gz", hostname: "data.iana.org") do
83-
%HTTPResponse{body: body, status_code: 200} ->
84-
Logger.info("Tz download done")
85-
{:ok, body}
86-
_ ->
87-
:error
77+
defp download_tz_database(version) do
78+
Logger.info("Tz is downloading the latest IANA time zone database (version #{version})...")
79+
case HTTPClient.request("GET", "/time-zones/releases/tzdata#{version}.tar.gz", hostname: "data.iana.org") do
80+
%HTTPResponse{body: body, status_code: 200} ->
81+
Logger.info("Tz download done")
82+
{:ok, body}
83+
_ ->
84+
:error
85+
end
8886
end
89-
end
9087

91-
defp extract_tz_database(version, content) do
92-
tmp_archive_path = Path.join(:code.priv_dir(:tz), "tzdata#{version}.tar.gz")
93-
tz_data_dir = "tzdata#{version}"
94-
:ok = File.write!(tmp_archive_path, content)
95-
:ok = :erl_tar.extract(tmp_archive_path, [:compressed, {:cwd, Path.join(:code.priv_dir(:tz), tz_data_dir)}])
96-
:ok = File.rm!(tmp_archive_path)
97-
end
88+
defp extract_tz_database(version, content) do
89+
tmp_archive_path = Path.join(:code.priv_dir(:tz), "tzdata#{version}.tar.gz")
90+
tz_data_dir = "tzdata#{version}"
91+
:ok = File.write!(tmp_archive_path, content)
92+
:ok = :erl_tar.extract(tmp_archive_path, [:compressed, {:cwd, Path.join(:code.priv_dir(:tz), tz_data_dir)}])
93+
:ok = File.rm!(tmp_archive_path)
94+
end
9895

99-
defp delete_tz_database(version) do
100-
Path.join(:code.priv_dir(:tz), "tzdata#{version}")
101-
|> File.rm_rf!()
96+
defp delete_tz_database(version) do
97+
Path.join(:code.priv_dir(:tz), "tzdata#{version}")
98+
|> File.rm_rf!()
99+
end
102100
end
103101
end

lib/watch_periodically.ex

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,50 @@
1-
defmodule Tz.WatchPeriodically do
2-
use GenServer
1+
if Code.ensure_loaded?(Mint.HTTP) do
2+
defmodule Tz.WatchPeriodically do
3+
use GenServer
34

4-
require Logger
5+
require Logger
56

6-
alias Tz.PeriodsProvider
7-
alias Tz.HTTP.HTTPClient
8-
alias Tz.HTTP.HTTPResponse
7+
alias Tz.PeriodsProvider
8+
alias Tz.HTTP.HTTPClient
9+
alias Tz.HTTP.HTTPResponse
910

10-
def start_link(_) do
11-
unless Code.ensure_loaded?(Mint.HTTP) do
12-
raise "Add mint to mix.exs to enable automatic time zone data updates"
11+
def start_link(_) do
12+
GenServer.start_link(__MODULE__, %{})
1313
end
1414

15-
GenServer.start_link(__MODULE__, %{})
16-
end
17-
18-
def init(state) do
19-
schedule_work()
20-
{:ok, state}
21-
end
22-
23-
def handle_info(:work, state) do
24-
Logger.debug("Tz is checking for IANA time zone database updates")
25-
26-
case fetch_iana_tz_version() do
27-
{:ok, latest_version} ->
28-
if latest_version != PeriodsProvider.version() do
29-
link = "https://data.iana.org/time-zones/releases/tzdata#{latest_version}.tar.gz"
30-
Logger.warn("Tz found a more recent time zone database available for download at #{link}")
31-
end
32-
:error ->
33-
Logger.error("Tz failed to read the latest version of the IANA time zone database")
15+
def init(state) do
16+
schedule_work()
17+
{:ok, state}
3418
end
3519

36-
schedule_work()
37-
{:noreply, state}
38-
end
20+
def handle_info(:work, state) do
21+
Logger.debug("Tz is checking for IANA time zone database updates")
22+
23+
case fetch_iana_tz_version() do
24+
{:ok, latest_version} ->
25+
if latest_version != PeriodsProvider.version() do
26+
link = "https://data.iana.org/time-zones/releases/tzdata#{latest_version}.tar.gz"
27+
Logger.warn("Tz found a more recent time zone database available for download at #{link}")
28+
end
29+
:error ->
30+
Logger.error("Tz failed to read the latest version of the IANA time zone database")
31+
end
32+
33+
schedule_work()
34+
{:noreply, state}
35+
end
3936

40-
defp schedule_work() do
41-
Process.send_after(self(), :work, 24 * 60 * 60 * 1000) # In 24 hours
42-
end
37+
defp schedule_work() do
38+
Process.send_after(self(), :work, 24 * 60 * 60 * 1000) # In 24 hours
39+
end
4340

44-
defp fetch_iana_tz_version() do
45-
case HTTPClient.request("GET", "/time-zones/tzdb/version", hostname: "data.iana.org") do
46-
%HTTPResponse{body: body, status_code: 200} ->
47-
{:ok, body |> List.first() |> String.trim()}
48-
_ ->
49-
:error
41+
defp fetch_iana_tz_version() do
42+
case HTTPClient.request("GET", "/time-zones/tzdb/version", hostname: "data.iana.org") do
43+
%HTTPResponse{body: body, status_code: 200} ->
44+
{:ok, body |> List.first() |> String.trim()}
45+
_ ->
46+
:error
47+
end
5048
end
5149
end
5250
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Tz.MixProject do
22
use Mix.Project
33

4-
@version "0.7.0"
4+
@version "0.7.1"
55

66
def project do
77
[

0 commit comments

Comments
 (0)