How to convert String to Ecto.DateTime in Elixir?

I need to convert a string containing the actual UTC time to Ecto.DateTimewhich I insert into my database with the correct format later. I tried to use the method Ecto.DateTime.cast(date), but it does not work. String Sat Aug 04 11:48:27 +0000 2012and comes from the Twitter API.

I know that there are libraries like Timex that I have not tested yet. Does Elixir have an easy working solution?

+4
source share
1 answer

Elixir or Erlang does not have a built-in solution for parsing the DateTimevalues ​​of this format:

Sat Aug 04 11:48:27 +0000 2012

, . , , , , Elixir/Erlang DateTime, , , Ecto.DateTime. . :


Timex - .

, Date/Time. Timex :

"Sat Aug 04 11:48:27 +0000 2012"
|> Timex.parse!("%a %b %d %T %z %Y", :strftime)
|> Ecto.DateTime.cast!

# => #Ecto.DateTime<2012-08-04 11:48:27>

. Timex DateTime, , DateTime, Twitter, , . , , ? Timex Parsing Formatting.

+7

Source: https://habr.com/ru/post/1659961/


All Articles