Skip to content

Commit 640c41d

Browse files
authored
feat: support :time decode (#58)
1 parent 51f7db7 commit 640c41d

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

lib/ecto/adapters/sqlite3.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ defmodule Ecto.Adapters.SQLite3 do
239239
[&Codec.naive_datetime_decode/1, type]
240240
end
241241

242+
@impl Ecto.Adapter
243+
def loaders(:time, type) do
244+
[&Codec.time_decode/1, type]
245+
end
246+
242247
@impl Ecto.Adapter
243248
def loaders(:utc_datetime_usec, type) do
244249
[&Codec.utc_datetime_decode/1, type]

lib/ecto/adapters/sqlite3/codec.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ defmodule Ecto.Adapters.SQLite3.Codec do
7272
end
7373
end
7474

75+
def time_decode(nil), do: {:ok, nil}
76+
77+
def time_decode(value) do
78+
case Time.from_iso8601(value) do
79+
{:ok, _time} = result -> result
80+
{:error, _} -> :error
81+
end
82+
end
83+
7584
def json_encode(value) do
7685
Application.get_env(:ecto_sqlite3, :json_library, Jason).encode(value)
7786
end

test/ecto/adapters/sqlite3/codec_test.exs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,32 @@ defmodule Ecto.Adapters.SQLite3.CodecTest do
8181
end
8282
end
8383

84+
describe ".time_decode/1" do
85+
test "nil" do
86+
{:ok, nil} = Codec.time_decode(nil)
87+
end
88+
89+
test "string" do
90+
{:ok, time} = Time.from_iso8601("23:50:07")
91+
assert {:ok, ^time} = Codec.time_decode("23:50:07")
92+
93+
{:ok, time} = Time.from_iso8601("23:50:07Z")
94+
assert {:ok, ^time} = Codec.time_decode("23:50:07Z")
95+
96+
{:ok, time} = Time.from_iso8601("T23:50:07Z")
97+
assert {:ok, ^time} = Codec.time_decode("T23:50:07Z")
98+
99+
{:ok, time} = Time.from_iso8601("23:50:07,0123456")
100+
assert {:ok, ^time} = Codec.time_decode("23:50:07,0123456")
101+
102+
{:ok, time} = Time.from_iso8601("23:50:07.0123456")
103+
assert {:ok, ^time} = Codec.time_decode("23:50:07.0123456")
104+
105+
{:ok, time} = Time.from_iso8601("23:50:07.123Z")
106+
assert {:ok, ^time} = Codec.time_decode("23:50:07.123Z")
107+
end
108+
end
109+
84110
describe ".utc_datetime_decode/1" do
85111
test "nil" do
86112
assert {:ok, nil} = Codec.utc_datetime_decode(nil)

0 commit comments

Comments
 (0)