Skip to content

Commit 428a5f2

Browse files
LostKobrakaiBenjamin Milde
andauthored
Handle only UTC datetimes and convert to iso form without offset (#157)
* Handle only UTC datetimes and convert to iso form without offset Co-authored-by: Benjamin Milde <[email protected]>
1 parent ab64f59 commit 428a5f2

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

lib/exqlite/sqlite3.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,13 @@ defmodule Exqlite.Sqlite3 do
167167
end
168168

169169
defp convert(%Date{} = val), do: Date.to_iso8601(val)
170-
defp convert(%DateTime{} = val), do: DateTime.to_iso8601(val)
171170
defp convert(%Time{} = val), do: Time.to_iso8601(val)
172171
defp convert(%NaiveDateTime{} = val), do: NaiveDateTime.to_iso8601(val)
172+
defp convert(%DateTime{time_zone: "Etc/UTC"} = val), do: NaiveDateTime.to_iso8601(val)
173+
174+
defp convert(%DateTime{} = datetime) do
175+
raise ArgumentError, "#{inspect(datetime)} is not in UTC"
176+
end
177+
173178
defp convert(val), do: val
174179
end

test/exqlite/sqlite3_test.exs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,25 @@ defmodule Exqlite.Sqlite3Test do
136136
{:ok, statement} = Sqlite3.prepare(conn, "insert into test (stuff) values (?1)")
137137
:ok = Sqlite3.bind(conn, statement, [Date.utc_today()])
138138
end
139+
140+
test "raises an error when binding non UTC datetimes" do
141+
{:ok, conn} = Sqlite3.open(":memory:")
142+
143+
:ok =
144+
Sqlite3.execute(conn, "create table test (id integer primary key, stuff text)")
145+
146+
{:ok, statement} = Sqlite3.prepare(conn, "insert into test (stuff) values (?1)")
147+
148+
msg = "#DateTime<2021-08-25 13:23:25+00:00 UTC Europe/Berlin> is not in UTC"
149+
150+
assert_raise ArgumentError, msg, fn ->
151+
{:ok, dt} = DateTime.new(~D[2021-08-25], ~T[13:23:25], "Etc/UTC")
152+
# Sneak in other timezone without a tz database
153+
other_tz = struct(dt, time_zone: "Europe/Berlin")
154+
155+
Sqlite3.bind(conn, statement, [other_tz])
156+
end
157+
end
139158
end
140159

141160
describe ".columns/2" do

0 commit comments

Comments
 (0)