How to convert a String into an Ecto.DateTime in Elixir? -
i need convert string containing valid utc time ecto.datetime
one, insert database correct format later. have tried using ecto.datetime.cast(date)
method doesn't seem work. string sat aug 04 11:48:27 +0000 2012
, comes twitter api.
i know there libraries such timex didn't inspect yet. there easy working solution built in elixir?
there's no built-in solution in elixir or erlang parsing datetime
values of format:
sat aug 04 11:48:27 +0000 2012
you can write parser yourself, it's neither going short or simple. you'll have split string, values of both date , time parameters, convert month strings month integers, parse timezone, represent complete value in elixir/erlang datetime formats , cast ecto.datetime
. see following links:
- elixir tips - date parsing
- erlang - how can parse rfc1123 dates erlang term?
- convert timestamp datetime in erlang
using timex best option here.
it's written library allows stay away chaos of inner workings of date/time. timex
, can parse string this:
"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>
note: timex has built-in support lot of common datetime formats, , found weird datetime format being sent twitter wasn't supported - wrote 1 you. maybe double check see if string correct? take @ timex parsing , formatting documentation.
Comments
Post a Comment