notsure

notsure

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?

Marked As Solved

LostKobrakai

LostKobrakai

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.

Last Post!

notsure

notsure

I was missing the Tzdata — tzdata v1.1.4 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!

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New

We're in Beta

About us Mission Statement