Easily parse timestamps using Timex as they are decoded from json by Poison.
If available in Hex, the package can be installed as:
- Add
timex_poison
to your list of dependencies inmix.exs
:
def deps do
[{:timex_poison, "~> 0.2.0"}]
end
- Ensure
timex_poison
is started before your application:
def application do
[applications: [:timex_poison]]
end
By default, timestamps will be parsed using Timex's {ISOz}
formatter.
defmodule MyGreatStruct do
use TimexPoison keys: [:created_at]
defstruct [:name, :created_at]
end
iex> Poison.decode! ~s({"name": "Great", "created_at": "2016-07-27T08:50:08.681Z"}), as: %MyGreatStruct{}
%MyGreatStruct{name: "Great", created_at: #<DateTime(2016-07-27T08:50:08Z)>}
Alternatively you can specify your own formatter:
defmodule WithDateFormat do
use TimexPoison, keys: [:timestamp], format: "{RFC1123}"
defstruct [:timestamp]
end
iex> Poison.decode(~s({"timestamp" : "Tue, 05 Mar 2013 23:25:19 +0200"}), as: %WithDateFormat{})