Timex.to_datetime() does not return %Timex.AmbiguousDateTime{}

Hello all and thanks in advance!

I am writing tests to handle time changes from DST and back. I cannot seem to trigger the %Timex.Ambiguous{} response from Timex.to_datetime():

iex(10)> Timex.to_datetime({{2023, 11, 5},{01, 30, 00}}, "US/Mountain") 
#DateTime<2023-11-05 01:30:00-06:00 MDT US/Mountain>

I originally was setup on {:timex, "~> 3.6.1"}, where I was able to trigger the correct response, but then I upgraded to {:timex, "~> 3.7.11"}. I don’t know if that change caused the response to change as I was not looking for it (was still learning how Timex works). But now that I know what I am looking for I have done a mix.deps clean --all and rebuilt to {:timex, "~> 3.6.1"}. I still do not get the expected result.

Any ideas what I am doing wrong?

I’d suggest sticking to core APIs wherever possible:

iex(1)> DateTime.new(~D[2023-11-05], ~T[01:30:00], "US/Mountain")
{:ambiguous, #DateTime<2023-11-05 01:30:00-06:00 MDT US/Mountain>,
 #DateTime<2023-11-05 01:30:00-07:00 MST US/Mountain>}

Edit: This requires a timezone database (like tzdata) to be installed and configured for usage.

2 Likes

It can only work with tzdata as it has been designed for the moment.

I was missing the Tzdata — tzdata v1.1.1 piece. Followed the directions to setup tzdata. Although when Timex is installed, it does install the tzdata dependency, but setting up tzdata requires more effort. Even with installing tzdata and performing the setup, I was not able to get the Ambiguous response with Timex but was able to with DateTime.new():

  def test do
    ts = timestamp(~D[2023-03-12], ~T[01:30:00], "US/Mountain")
    IO.inspect(ts, label: "test a valid time")
    ts = timestamp(~D[2023-03-12], ~T[02:30:00], "US/Mountain")
    IO.inspect(ts, label: "test a time that should not exist")
    ts = timestamp(~D[2023-11-05], ~T[01:30:00], "US/Mountain")
    IO.inspect(ts, label: "test a time that is ambiguous")
  end
  def timestamp(date, time, time_zone) do
    case DateTime.new(date, time, time_zone) do
      {:ok, timestamp} -> timestamp
      {:gap, timestamp1, timestamp2} -> # spring ahead, time in hour gap
        timestamp(date, Time.add(time, 3600, :second), time_zone)
      {:ambiguous, timestamp1, timestamp2} -> # fall back; which 1:30?
        timestamp1
      timestamp -> timestamp
    end
  end

Thanks for all the help and quick response!