Phx.gen.live mix task with Tzdata.TimeZoneDatabase fails with :time_zone_not_found

When I run mix phx.gen.live ... with :time_zone_database set in config.exs the task will raise with the following error:

** (ArgumentError) cannot add -86400 second to ~U[2021-09-30 19:07:00Z] (with time zone database Tzdata.TimeZoneDatabase), reason: :time_zone_not_found
    (elixir 1.12.3) lib/calendar/datetime.ex:1391: DateTime.add/4
    lib/mix/phoenix/schema.ex:195: anonymous fn/2 in Mix.Phoenix.Schema.params/2
    (elixir 1.12.3) lib/enum.ex:1582: Enum."-map/2-lists^map/1-0-"/2
    (elixir 1.12.3) lib/enum.ex:1582: Enum."-map/2-lists^map/1-0-"/2
    (elixir 1.12.3) lib/enum.ex:1485: Enum.into/3
    lib/mix/phoenix/schema.ex:95: Mix.Phoenix.Schema.new/4
    lib/mix/tasks/phx.gen.context.ex:116: Mix.Tasks.Phx.Gen.Context.build/2
    lib/mix/tasks/phx.gen.live.ex:93: Mix.Tasks.Phx.Gen.Live.run/1

In my config.exs I have:

config :elixir, :time_zone_database, Tzdata.TimeZoneDatabase

If I comment out that line, the mix task will succeed.

At first I thought it maybe it was something in my project causing the issue. To rule it out I created a new Phoenix 1.6 project with mix phx.new date_test and added {:tzdata, "~> 1.1"} as a dependency in mix.exs, added the :time_zone_database in config.exs, ran mix deps.get, and then started the server to make sure that tzdata fetched the timezone info etc.

I shut down the server and ran the following mix task which raised with the error above:

$ mix phx.gen.live Tests Test tests title:string test_date:utc_datetime

Commenting the line in config.exs will allow it to succeed.

I don’t use the Phoenix generators very often, but in this case I was working on a quick project idea when I came across this issue. Am I just missing something here; or is this a possible bug?

Phoenix.Schema is just calling DateTime.utc_now/1, which should have “Etc/UTC” timezone. Since that is a valid timezone in tzdata, the database must not be loading properly.

1 Like

Yes. That’s what I concluded as well. Tzdata cannot load the ETS table as mentioned in the Timex README.md:

If you need to use Timex from within an escript, add {:tzdata, “~> 0.1.8”, override: true} to your deps, more recent versions of :tzdata are unable to work in an escript because of the need to load ETS table files from priv, and due to the way ETS loads these files, it’s not possible to do so.

Changing the project to use Tz as the :time_zone_database solved the issue and allowed the mix task to succeed.

1 Like