bitwalker / timex_ecto

An adapter for using Timex DateTimes with Ecto
MIT License
162 stars 68 forks source link

Support for casting from strings #55

Closed wmnnd closed 7 years ago

wmnnd commented 7 years ago

Hi there,

I was trying out timex_ecto and now I have encountered the problem that I can’t seem to cast ISO 8601 strings into Timex.Ecto.Datetime through a changeset.

Say I have the following Ecto Schema:

defmodule DateTimeTest do
  use Ecto.Schema
  import Ecto.Changeset

  schema "date_time_test" do
    field :timex, Timex.Ecto.DateTime
    field :ecto, Ecto.DateTime
  end

  def changeset(struct, params) do
    cast(struct, params, [:timex, :ecto])
  end
end

When I try to cast ISO 8601 strings, it works fine with Ecto.DateTime while Timex.Ecto.DateTime fails:

iex> DateTimeTest.changeset(%DateTimeTest{}, %{timex: "2016-12-19T02:23:05Z", ecto: "2016-12-19T02:23:05Z"})
# #Ecto.Changeset<action: nil,
#  changes: %{ecto: #Ecto.DateTime<2016-12-19 02:23:05>},
#  errors: [timex: {"is invalid",
#    [type: Timex.Ecto.DateTime, validation: :cast]}], data: #DateTimeTest<>,
#  valid?: false>

Am I missing something here? Unfortunately, the Timex docs don’t mention changesets at all.

wmnnd commented 7 years ago

Some more context: I’m trying this with Elixir 1.3.3, timex_ecto 3.0.5 and timex 3.1.5. Researching the problem, I also tried the example given in #16 and it doesn’t work either:

iex> Timex.Ecto.DateTime.cast!("2016-02-02T12:30:00Z")
# :error
bitwalker commented 7 years ago

Could you upgrade to timex_ecto 3.1.0? Your example code works as expected there.

wmnnd commented 7 years ago

Where would I find that version? timex_ecto’s latest version on Hex is 3.0.5 just like here on GitHub.

bitwalker commented 7 years ago

I just published it, sorry about that!