BrightEyesDavid

BrightEyesDavid

From shifted DateTime to Cldr.DateTime.to_string/2

I have a UTC datetime from a timestamptz PostgreSQL column, and am trying to:

  1. convert it to the user’s timezone;
  2. output a localised string representation for the frontend.

I’m trying to use DateTime for 1 and Cldr.DateTime for 2.

iex> {:ok, shifted} = DateTime.shift_zone(~U[2024-07-13 21:33:23.163097Z], "Europe/Berlin")
{:ok, #DateTime<2024-07-13 23:33:23.163097+02:00 CEST Europe/Berlin>}

However, Cldr.DateTime.to_string/2 doesn’t accept #DateTime<...>:

iex> {:ok, result} = Cldr.DateTime.to_string(shifted, "en-GB")
** (MatchError) no match of right hand side value: {:error, {ArgumentError, "Invalid DateTime. DateTime is a map that contains at least :year, :month, :day, :hour, :minute, :second and :calendar. Found: #DateTime<2024-07-13 23:33:23.163097+02:00 CEST Europe/Berlin Cldr.Calendar.Gregorian>"}}
    (stdlib 4.3.1.3) erl_eval.erl:496: :erl_eval.expr/6
    iex:3: (file)

I don’t know what #...<...> means, exactly, or how to convert it to a DateTime struct/map that Cldr.DateTime can use. Thanks.

Marked As Solved

belaustegui

belaustegui

Looks like you are calling Cldr.DateTime.to_string incorrectly.

The locale must be passed as a keyword list. This should work:

{:ok, result} = Cldr.DateTime.to_string(shifted, locale: "en-GB")

Also Liked

kip

kip

ex_cldr Core Team

I’ve published ex_cldr_dates_times version 2.20.2 that improves the error message when options are not provided as a keyword list. Using your example:

iex> {:ok, shifted} = DateTime.shift_zone(~U[2024-07-13 21:33:23.163097Z], "Europe/Berlin")
{:ok, #DateTime<2024-07-13 23:33:23.163097+02:00 CEST Europe/Berlin>}

iex> Cldr.DateTime.to_string(shifted, "en-GB")
{:error,
 {ArgumentError,
  "Unexpected option value \"en-GB\". Options must be a keyword list"}}

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement