devinus / poison

An incredibly fast, pure Elixir JSON library
BSD Zero Clause License
2.02k stars 218 forks source link

Support decode datetime #180

Open jeffhsta opened 5 years ago

jeffhsta commented 5 years ago

Linked to https://github.com/devinus/poison/issues/174

To use the datetime decode feature is just need to request it in the options, like:

%{now: Time.utc_now()}
|> Poison.encode!()
|> Poison.decode!(format_datetime: :time)

# %{"now" => ~T[02:24:23.871934]}

%{now: Date.utc_today()}
|> Poison.encode!()
|> Poison.decode!(format_datetime: :date)

# %{"now" => ~D[2018-10-11]}

%{now: DateTime.utc_now()}
|> Poison.encode!()
|> Poison.decode!(format_datetime: :datetime)

# %{"now" => #DateTime<2018-10-11 02:25:05.251816Z>}

%{now: NaiveDateTime.utc_now()}
|> Poison.encode!()
|> Poison.decode!(format_datetime: :naive_datetime)

# %{"now" => ~N[2018-10-11 02:25:13.460277]}
coveralls commented 5 years ago

Pull Request Test Coverage Report for Build 245


Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/poison/parser.ex 21 27 77.78%
<!-- Total: 21 27 77.78% -->
Totals Coverage Status
Change from base Build 236: -1.03%
Covered Lines: 198
Relevant Lines: 224

💛 - Coveralls