Skip to content

Commit

Permalink
Support TZDIR environment variable
Browse files Browse the repository at this point in the history
This uses the more standard `$TZDIR` environment variable for overriding
the zoneinfo directory. If `$TZDIR` isn't specified, the original
`$TZPATH` is used for backwards compatibility.

Fixes #34.
  • Loading branch information
fhunleth committed Dec 22, 2023
1 parent 7d2c0e2 commit 5e4c9eb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The final step is to specify the location of the time zone files. Zoneinfo looks
at the following locations:

1. The `:tzpath` key in the application environment
2. The `TZPATH` environment variable
2. The `TZDIR` environment variable
3. `/usr/share/zoneinfo`

Since `/usr/share/zoneinfo` is the default on Linux and OSX, you may not need to
Expand Down
5 changes: 4 additions & 1 deletion lib/zoneinfo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Zoneinfo do
Time zone data is loaded from the path returned by `tzpath/0`. The default
is to use `/usr/share/zoneinfo`, but that may be changed by setting the
`$TZPATH` environment or adding the following to your project's `config.exs`:
`$TZDIR` environment or adding the following to your project's `config.exs`:
```elixir
config :zoneinfo, tzpath: "/custom/location"
Expand Down Expand Up @@ -45,7 +45,10 @@ defmodule Zoneinfo do
"""
@spec tzpath() :: binary()
def tzpath() do
# TZDIR is preferred. TZPATH was originally used and is supported for
# backwards compatibility.
with nil <- Application.get_env(:zoneinfo, :tzpath),
nil <- System.get_env("TZDIR"),
nil <- System.get_env("TZPATH") do
"/usr/share/zoneinfo"
end
Expand Down
10 changes: 10 additions & 0 deletions test/zoneinfo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ defmodule ZoneinfoTest do
end

test "OS environment" do
old_path = clear_path()
System.put_env("TZDIR", "tzpath_environment")

assert Zoneinfo.tzpath() == "tzpath_environment"

:os.unsetenv(~c"TZDIR")
pop_path(old_path)
end

test "deprecated TZPATH OS environment" do
old_path = clear_path()
System.put_env("TZPATH", "tzpath_environment")

Expand Down

0 comments on commit 5e4c9eb

Please sign in to comment.