Skip to content

Commit acbd41b

Browse files
committed
Fix date loader and dumper
1 parent 2b931b5 commit acbd41b

2 files changed

Lines changed: 23 additions & 25 deletions

File tree

lib/mongo_ecto.ex

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -437,29 +437,22 @@ defmodule Mongo.Ecto do
437437
def loaders(:binary, type), do: [&load_binary/1, type]
438438
def loaders(_base, type), do: [type]
439439

440-
defp load_time(time),
441-
do: Time.to_erl(time)
442-
defp load_time(_),
443-
do: :error
440+
defp load_time(time), do: Time.to_erl(time)
441+
defp load_time(_), do: :error
444442

445-
defp load_date(date),
446-
do: Date.from_erl(date)
447-
defp load_date(_),
448-
do: :error
443+
defp load_date(date), do: {:ok, date |> DateTime.to_date() |> Date.to_erl()}
444+
defp load_date(date), do: :error
449445

450446
defp load_datetime(datetime) do
451447
naive = DateTime.to_naive(datetime)
452448
{date, {h, m, s}} = NaiveDateTime.to_erl(naive)
453449
{x, _} = naive.microsecond
454450
{:ok, {date, {h, m, s, x}}}
455451
end
456-
defp load_datetime(_),
457-
do: :error
452+
defp load_datetime(_), do: :error
458453

459-
defp load_binary(%BSON.Binary{binary: binary}),
460-
do: {:ok, binary}
461-
defp load_binary(_),
462-
do: :error
454+
defp load_binary(%BSON.Binary{binary: binary}), do: {:ok, binary}
455+
defp load_binary(_), do: :error
463456

464457
defp load_objectid(%BSON.ObjectId{} = objectid) do
465458
try do
@@ -486,8 +479,13 @@ defmodule Mongo.Ecto do
486479
defp dump_time(_),
487480
do: :error
488481

489-
defp dump_date({_, _, _} = date),
490-
do: Date.from_erl!(date)
482+
defp dump_date({_, _, _} = date) do
483+
dt = {date, {0, 0, 0}}
484+
|> NaiveDateTime.from_erl!()
485+
|> datetime_from_naive!("Etc/UTC")
486+
487+
{:ok, dt}
488+
end
491489
defp dump_date(_),
492490
do: :error
493491

mix.lock

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
%{"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"},
1+
%{"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], []},
22
"db_connection": {:git, "https://github.com/elixir-ecto/db_connection.git", "24473f673760ded3636f3086e0e2d768605ea1e8", [ref: "24473f6"]},
3-
"decimal": {:hex, :decimal, "1.4.0", "fac965ce71a46aab53d3a6ce45662806bdd708a4a95a65cde8a12eb0124a1333", [:mix], [], "hexpm"},
4-
"dialyze": {:hex, :dialyze, "0.2.1", "9fb71767f96649020d769db7cbd7290059daff23707d6e851e206b1fdfa92f9d", [:mix], [], "hexpm"},
5-
"earmark": {:hex, :earmark, "1.2.3", "206eb2e2ac1a794aa5256f3982de7a76bf4579ff91cb28d0e17ea2c9491e46a4", [:mix], [], "hexpm"},
6-
"ecto": {:hex, :ecto, "2.1.6", "29b45f393c2ecd99f83e418ea9b0a2af6078ecb30f401481abac8a473c490f84", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},
7-
"ex_doc": {:hex, :ex_doc, "0.16.2", "3b3e210ebcd85a7c76b4e73f85c5640c011d2a0b2f06dcdf5acdb2ae904e5084", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"},
8-
"inch_ex": {:hex, :inch_ex, "0.5.6", "418357418a553baa6d04eccd1b44171936817db61f4c0840112b420b8e378e67", [:mix], [{:poison, "~> 1.5 or ~> 2.0 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
3+
"decimal": {:hex, :decimal, "1.4.0", "fac965ce71a46aab53d3a6ce45662806bdd708a4a95a65cde8a12eb0124a1333", [:mix], []},
4+
"dialyze": {:hex, :dialyze, "0.2.1", "9fb71767f96649020d769db7cbd7290059daff23707d6e851e206b1fdfa92f9d", [:mix], []},
5+
"earmark": {:hex, :earmark, "1.2.3", "206eb2e2ac1a794aa5256f3982de7a76bf4579ff91cb28d0e17ea2c9491e46a4", [:mix], []},
6+
"ecto": {:hex, :ecto, "2.1.6", "29b45f393c2ecd99f83e418ea9b0a2af6078ecb30f401481abac8a473c490f84", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, optional: true]}]},
7+
"ex_doc": {:hex, :ex_doc, "0.16.2", "3b3e210ebcd85a7c76b4e73f85c5640c011d2a0b2f06dcdf5acdb2ae904e5084", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, optional: false]}]},
8+
"inch_ex": {:hex, :inch_ex, "0.5.6", "418357418a553baa6d04eccd1b44171936817db61f4c0840112b420b8e378e67", [:mix], [{:poison, "~> 1.5 or ~> 2.0 or ~> 3.0", [hex: :poison, optional: false]}]},
99
"mongodb": {:git, "https://github.com/ankhers/mongodb.git", "d2d0954da6385a525d8d3577d28c19fb0b215349", [branch: "reboot_replica_sets"]},
10-
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
11-
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], [], "hexpm"}}
10+
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], []},
11+
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []}}

0 commit comments

Comments
 (0)