Skip to content

Commit 83d4a32

Browse files
committed
Fix datetime serialization format via :datetime_type config
Thank you @krwenholz for pointing this out. closes: #116
1 parent 826b052 commit 83d4a32

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The format is loosely based on [Keep a Changelog][keepachangelog], and this
66
project adheres to [Semantic Versioning][semver].
77

88
## Unreleased
9+
- fixed: Handle datetime serialization format via `:datetime_type` config.
910

1011
## v0.17.1
1112
- changed: Bump minimum ecto to `3.12`.

lib/ecto/adapters/sqlite3/codec.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,16 @@ defmodule Ecto.Adapters.SQLite3.Codec do
114114
end
115115

116116
@text_datetime_format "%Y-%m-%d %H:%M:%S"
117+
@iso8601_format "%Y-%m-%dT%H:%M:%S"
118+
119+
def datetime_format(:text_datetime), do: @text_datetime_format
120+
def datetime_format(_), do: @iso8601_format
117121

118122
def utc_datetime_encode(nil, :iso8601), do: {:ok, nil}
119123
def utc_datetime_encode(nil, :text_datetime), do: {:ok, nil}
120124

121125
def utc_datetime_encode(%{time_zone: "Etc/UTC"} = value, :iso8601) do
122-
{:ok, NaiveDateTime.to_iso8601(value)}
126+
{:ok, Calendar.strftime(value, @iso8601_format)}
123127
end
124128

125129
def utc_datetime_encode(%{time_zone: "Etc/UTC"} = value, :text_datetime) do
@@ -135,7 +139,7 @@ defmodule Ecto.Adapters.SQLite3.Codec do
135139
def naive_datetime_encode(nil, :text_datetime), do: {:ok, nil}
136140

137141
def naive_datetime_encode(value, :iso8601) do
138-
{:ok, NaiveDateTime.to_iso8601(value)}
142+
{:ok, Calendar.strftime(value, @iso8601_format)}
139143
end
140144

141145
def naive_datetime_encode(value, :text_datetime) do

lib/ecto/adapters/sqlite3/connection.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1369,10 +1369,14 @@ defmodule Ecto.Adapters.SQLite3.Connection do
13691369
[quote_name(name)]
13701370
end
13711371

1372+
@datetime_type Application.compile_env(:ecto_sqlite3, :datetime_type, :iso8601)
1373+
13721374
defp expr({:datetime_add, _, [datetime, count, interval]}, sources, query) do
1375+
format = Ecto.Adapters.SQLite3.Codec.datetime_format(@datetime_type)
1376+
13731377
[
13741378
"CAST (",
1375-
"strftime('%Y-%m-%d %H:%M:%f000Z'",
1379+
"strftime('#{format}'",
13761380
",",
13771381
expr(datetime, sources, query),
13781382
",",

test/ecto/adapters/sqlite3/connection/datetime_add_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule Ecto.Adapters.SQLite3.Connection.DatetimeAddTest do
1111
|> select([], true)
1212
|> plan()
1313

14-
assert ~s{SELECT 1 FROM "schema" AS s0 WHERE (CAST (strftime('%Y-%m-%d %H:%M:%f000Z',s0.\"foo\",1 || ' month') AS TEXT) > s0."bar")} ==
14+
assert ~s{SELECT 1 FROM "schema" AS s0 WHERE (CAST (strftime('%Y-%m-%dT%H:%M:%S',s0.\"foo\",1 || ' month') AS TEXT) > s0."bar")} ==
1515
all(query)
1616
end
1717

@@ -22,7 +22,7 @@ defmodule Ecto.Adapters.SQLite3.Connection.DatetimeAddTest do
2222
|> select([], true)
2323
|> plan()
2424

25-
assert ~s{SELECT 1 FROM "schema" AS s0 WHERE (CAST (strftime('%Y-%m-%d %H:%M:%f000Z',CAST(s0.\"foo\" AS TEXT),1 || ' month') AS TEXT) > s0."bar")} ==
25+
assert ~s{SELECT 1 FROM "schema" AS s0 WHERE (CAST (strftime('%Y-%m-%dT%H:%M:%S',CAST(s0.\"foo\" AS TEXT),1 || ' month') AS TEXT) > s0."bar")} ==
2626
all(query)
2727
end
2828
end

test/ecto/integration/timestamps_test.exs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,31 @@ defmodule Ecto.Integration.TimestampsTest do
181181
|> TestRepo.all()
182182
end
183183

184+
test "using built in ecto functions" do
185+
account = insert_account(%{name: "Test"})
186+
187+
insert_product(%{
188+
account_id: account.id,
189+
name: "Foo",
190+
inserted_at: seconds_ago(1)
191+
})
192+
193+
insert_product(%{
194+
account_id: account.id,
195+
name: "Bar",
196+
inserted_at: seconds_ago(3)
197+
})
198+
199+
assert [
200+
%{name: "Foo"},
201+
] =
202+
Product
203+
|> select([p], p)
204+
|> where([p], p.inserted_at >= ago(2, "second"))
205+
|> order_by([p], desc: p.inserted_at)
206+
|> TestRepo.all()
207+
end
208+
184209
defp insert_account(attrs) do
185210
%Account{}
186211
|> Account.changeset(attrs)
@@ -192,4 +217,9 @@ defmodule Ecto.Integration.TimestampsTest do
192217
|> Product.changeset(attrs)
193218
|> TestRepo.insert!()
194219
end
220+
221+
defp seconds_ago(seconds) do
222+
now = DateTime.utc_now()
223+
DateTime.add(now, -seconds, :second)
224+
end
195225
end

0 commit comments

Comments
 (0)