BrightEyesDavid
From shifted DateTime to Cldr.DateTime.to_string/2
I have a UTC datetime from a timestamptz PostgreSQL column, and am trying to:
- convert it to the user’s timezone;
- 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
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
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"}}
5
Popular in Questions
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
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
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
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
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> somethi...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
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
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
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
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
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
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...
New
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
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
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
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








