Ecto utc_datetime does not work with Ecto

Hello there, I have table with a field :dayTime, :utc_datetime and I use Timex to convert the string that I take to a datetime, for example I have a string "29/02/2016 22:55:00" I parse it to ~U[2016-02-29 22:25:00Z] after doing some work then passing it to Timex.parse/2. My problem is when I try to create a new instance and save it to the database I get this error

errors: [dayTime: {"is invalid", [type: :utc_datetime, validation: :cast]}],
   data: #App.Context.Schema
   valid?: false

what am I doing wrong?

Thanks in advance.

Hi @aligredo,

Could you show an example of breaking code with:

  1. Your schema code
  2. Your changeset code
  3. The migration to create the tables in the database

Also let us know what version of Ecto & related projects (i.e. ecto_sql & postgrex) you are using (maybe just put the contents of mix.exs up).

Also, I note that the date you supply is 29th Feb 2016 which is valid because 2016 was a leap year. Out of interest, have you tried a different date just to help try to isolate the problem. There may be some date validation bug lurking in there, although I followed the code down to the days in month check for 2016 and it seems ok:

iex> Calendar.ISO.days_in_month(2016, 2)
29

FWIW - I have utc_datetime datatypes working fine

2 Likes

turns out that I was doing something wrong after conversion that caused that error, I fixed it.
Thank you for your time <3

1 Like