I fetched utc_now
date like:
NaiveDateTime.utc_now
and I got:
~N[2017-01-06 19:28:39.656722]
so I tried to create a NaiveDateTime
from its struct like:
result = %NaiveDateTime{day: 1, month: 1, year: 2017, hour: 0, minute: 0, second: 0.5}
and I got:
%Inspect.Error{message: "got ArgumentError with message \"argument error\" while inspecting \e[39m%{\e[0m\e[33m\e[36m__struct__: \e[0m\e[33m\e[36mNaiveDateTime\e[0m\e[33m\e[39m,\e[0m\e[33m \e[36mcalendar: \e[0m\e[33m\e[36mCalendar.ISO\e[0m\e[33m\e[39m,\e[0m\e[33m \e[36mday: \e[0m\e[33m1\e[39m,\e[0m\e[33m \e[36mhour: \e[0m\e[33m0\e[39m,\e[0m\e[33m \e[36mmicrosecond: \e[0m\e[33m\e[39m{\e[0m\e[33m0\e[39m,\e[0m\e[33m 0\e[39m}\e[0m\e[33m\e[39m,\e[0m\e[33m \e[36mminute: \e[0m\e[33m0\e[39m,\e[0m\e[33m \e[36mmonth: \e[0m\e[33m1\e[39m,\e[0m\e[33m \e[36msecond: \e[0m\e[33m0.5\e[39m,\e[0m\e[33m \e[36myear: \e[0m\e[33m2017\e[39m}\e[0m\e[33m"}
I was so surprised and tried to check it like:
IO.puts result.message
and was much more surprised when got:
** (KeyError) key :message not found in: %Inspect.Error{message: "got ArgumentError with message \"argument error\" while inspecting %{__struct__: NaiveDateTime, calendar: Calendar.ISO, day: 1, hour: 0, microsecond: {0, 0}, minute: 0, month: 1, second: 0.5, year: 2017}"}
After some thinking I understand that I can’t pass non-integer values to NaiveDateTime
struct, but still I have some questions:
- Is it possible to change way of displaying message from
Inspect.Error
(of course without raise like above)? - Can you explain me why I got
KeyError
?
My environment (iex output):
Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:4:4] [async-threads:10]
Interactive Elixir (1.4.0)
I have installed Erlang from my package manager (emerge). I have also installed stable (for my distribution) version of Elixir (1.3.0-r1).
Elixir 1.4.0 is compiled from source like:
git clone git@github.com:elixir-lang/elixir.git
cd elixir
git checkout tags/v1.4.0
make clean test
./bin/iex # Here I'm experimenting