Ross
Ross

Reputation: 3070

Why do I receive an ArgumentError in the Timex library Timex.compare/2 when passing the result from Timex.date/1

With Elixir 1.2.4 and using Timex 2.1.4 as a dependency I run the following code:

Timex.compare(Timex.date({2015, 12, 1}), Timex.date({2016,1,1}))

Which breaks with the error:

%ArgumentError{message: "argument error"}

Yet if I run:

Timex.compare(Timex.Date.now, Timex.Date.now)

I receive an expected value (0 or 'equals').

When I IO.inspect the results of my Timex.date/1 calls I'm seeing the same Date information I get from a Timex.Date.now call [albeit with different dates contained].

I tried a case ... do pattern match to see if I was discarding a more meaningful error in the {:error, term} return but that did not yield anything different.

This is likely due to my inexperience with Elixir, so what am I missing?


Full code:

defmodule Elixirlearning do

  n = Timex.date({2015, 12, 1})
  IO.inspect Timex.Date.now
  IO.inspect n
  IO.inspect Timex.compare(Timex.Date.now, Timex.Date.now)
  IO.inspect Timex.compare(Timex.date({2015, 12, 1}), Timex.date({2016,1,1}))

end

Output, as produced in spacemacs alchemist evaluation:

nofile:58: warning: redefining module Elixirlearning
#<Date(2016-04-20)>
#<Date(2015-12-01)>
0
%ArgumentError{message: "argument error"}

Just to clarify, this is using the 'alchemist-eval-buffer' option in the spacemacs version of emacs on OSX and producing the above output in a new window.

Here's a screenshot of everything I see in case it provides information I don't think of:

screenshot of spacemacs with alchemist

Upvotes: 0

Views: 591

Answers (1)

ash
ash

Reputation: 711

timex is probably not started. You need to ensure it is started:

Application.ensure_all_started :timex

iex(24)> Timex.compare(Timex.date({2015, 12, 1}), Timex.date({2015, 12, 2})) ** (ArgumentError) argument error (stdlib) :ets.lookup(:tzdata_current_release, :release_version) lib/tzdata/release_reader.ex:41: Tzdata.ReleaseReader.current_release_from_table/0 lib/tzdata/release_reader.ex:13: Tzdata.ReleaseReader.simple_lookup/1 lib/tzdata/release_reader.ex:7: Tzdata.ReleaseReader.zone_and_link_list/0 lib/tzdata.ex:61: Tzdata.zone_exists?/1 lib/timezone/timezone.ex:166: Timex.Timezone.do_get/3 lib/timezone/timezone.ex:310: Timex.Timezone.convert/2 lib/datetime/datetime.ex:532: Timex.DateTime.to_seconds/3

iex(25)> Application.ensure_all_started :timex

{:ok, [:idna, :mimerl, :certifi, :ssl_verify_fun, :metrics, :hackney, :tzdata, :gettext, :timex]}

iex(29)> Timex.compare(Timex.date({2015, 12, 1}), Timex.date({2015, 12, 2}))

-1

An easy way to ensure that this app is started is to list it as application dependency in mix.exs

Upvotes: 3

Related Questions