Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [Ecto.Query] Fix subqueries in interpolated join filters (this changes the representation of `JoinExpr.on` fields to a `BooleanExpr`)
* [Ecto.Query] Fix selecting lists of fields and using `map/2` from subqueries
* [Ecto.Type] Normalize `DateTime` casts to UTC for `:time` and `:time_usec`
* [Ecto.Type] Normalize ISO 8601 strings with a UTC offset to UTC when casting to `:date`, `:naive_datetime` and `:naive_datetime_usec`

## v3.14.2 (2026-08-14)

Expand Down
26 changes: 18 additions & 8 deletions lib/ecto/type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1062,9 +1062,9 @@ defmodule Ecto.Type do
ok

{:error, _} ->
case NaiveDateTime.from_iso8601(binary) do
case parse_naive_datetime(binary) do
{:ok, naive_datetime} -> {:ok, NaiveDateTime.to_date(naive_datetime)}
{:error, _} -> :error
:error -> :error
end
end
end
Expand Down Expand Up @@ -1199,12 +1199,8 @@ defmodule Ecto.Type do
end
end

defp cast_naive_datetime(binary) when is_binary(binary) do
case NaiveDateTime.from_iso8601(binary) do
{:ok, _} = ok -> ok
{:error, _} -> :error
end
end
defp cast_naive_datetime(binary) when is_binary(binary),
do: parse_naive_datetime(binary)

defp cast_naive_datetime(%DateTime{} = datetime) do
case cast_utc_datetime(datetime) do
Expand Down Expand Up @@ -1240,6 +1236,20 @@ defmodule Ecto.Type do
:error
end

# A string with a UTC offset is normalized to UTC, like a %DateTime{}.
defp parse_naive_datetime(binary) do
case DateTime.from_iso8601(binary) do
{:ok, datetime, _offset} ->
{:ok, DateTime.to_naive(datetime)}

{:error, _} ->
case NaiveDateTime.from_iso8601(binary) do
{:ok, _} = ok -> ok
{:error, _} -> :error
end
end
end

## UTC datetime

defp cast_utc_datetime("-" <> rest) do
Expand Down
8 changes: 8 additions & 0 deletions test/ecto/type_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ defmodule Ecto.TypeTest do

assert Ecto.Type.cast(:date, "2015-12-31T00:00:00") == {:ok, @date}
assert Ecto.Type.cast(:date, "2015-12-31 00:00:00") == {:ok, @date}
assert Ecto.Type.cast(:date, "2016-01-01T01:00:00+02:00") == {:ok, @date}
end

test "dump" do
Expand Down Expand Up @@ -653,6 +654,10 @@ defmodule Ecto.TypeTest do

assert Ecto.Type.cast(:naive_datetime, "2015-01-23T23:50:07.008000") == {:ok, @datetime}
assert Ecto.Type.cast(:naive_datetime, "2015-01-23T23:50:07.008000Z") == {:ok, @datetime}
assert Ecto.Type.cast(:naive_datetime, "2015-01-24T09:50:07+10:00") == {:ok, @datetime}

assert Ecto.Type.cast(:naive_datetime, "2015-01-23T17:50:07.008000-06:00") ==
{:ok, @datetime}

assert Ecto.Type.cast(:naive_datetime, "2015-01-23P23:50:07") == :error
assert Ecto.Type.cast(:naive_datetime, "2015-01-23 23:50.123") == :error
Expand Down Expand Up @@ -869,6 +874,9 @@ defmodule Ecto.TypeTest do
assert Ecto.Type.cast(:naive_datetime_usec, "2015-01-23T23:50:07.008000Z") ==
{:ok, @datetime_usec}

assert Ecto.Type.cast(:naive_datetime_usec, "2015-01-23T17:50:07.008000-06:00") ==
{:ok, @datetime_usec}

assert Ecto.Type.cast(:naive_datetime_usec, "2015-01-23P23:50:07") == :error
end

Expand Down
Loading