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
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
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
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
Hey guys.
I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
I would like to know what is the best IDE for elixir development?
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
I have a list say
x = ["23gh", "56kh", "97mh"]
I would like to pass each element to Val in each iteration.
Say, in iteration 1 -------...
New
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work.
Or rather, not char, but a substr...
New
Other popular topics
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
In Ruby, I can go:
User.find_by(email: "foobar@email.com").update(email: "hello@email.com")
How can I do something similar in Elixir? ...
New
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
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
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
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New







