Repository: phoenixframework/phoenix_html
Pull request #339 (2021-05-13 16:35:06)

Allow DateTime to be used with datetime_local_input/3
Paired with @supersimple on this one. 

Found this via testing with the Surface [DateTimeLocalInput](https://surface-ui.org/builtincomponents/DateTimeLocalInput) component. 

As for the expected behavior, this will not take any timezone into account. This seems reasonable to me, but happy to change based on feedback.


Patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 8a32a65..7253970 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -48,10 +48,6 @@ jobs:
       run: mix format --check-formatted
       if: ${{ matrix.lint }}
 
-    - name: Check unused dependencies
-      run: mix deps.unlock --check-unused
-      if: ${{ matrix.lint }}
-
     - name: Remove compiled application files
       run: mix clean
 
diff --git a/lib/phoenix_html/form.ex b/lib/phoenix_html/form.ex
index 215b819..51e5484 100644
--- a/lib/phoenix_html/form.ex
+++ b/lib/phoenix_html/form.ex
@@ -755,9 +755,8 @@ defmodule Phoenix.HTML.Form do
     generic_input(:"datetime-local", form, field, opts)
   end
 
-  defp datetime_local_input_value(%NaiveDateTime{} = value) do
-    <<date::10-binary, ?\s, hour_minute::5-binary, _rest::binary>> =
-      NaiveDateTime.to_string(value)
+  defp datetime_local_input_value(%struct{} = value) when struct in [NaiveDateTime, DateTime] do
+    <<date::10-binary, ?\s, hour_minute::5-binary, _rest::binary>> = struct.to_string(value)
 
     [date, ?T, hour_minute]
   end
diff --git a/mix.lock b/mix.lock
index 62e23ff..4ae4630 100644
--- a/mix.lock
+++ b/mix.lock
@@ -1,4 +1,5 @@
 %{
+  "earmark_parser": {:hex, :earmark_parser, "1.4.13", "0c98163e7d04a15feb62000e1a891489feb29f3d10cb57d4f845c405852bbef8", [:mix], [], "hexpm", "d602c26af3a0af43d2f2645613f65841657ad6efc9f0e361c3b6c06b578214ba"},
   "ex_doc": {:hex, :ex_doc, "0.23.0", "a069bc9b0bf8efe323ecde8c0d62afc13d308b1fa3d228b65bca5cf8703a529d", [:mix], [{:earmark_parser, "~> 1.4.0", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "f5e2c4702468b2fd11b10d39416ddadd2fcdd173ba2a0285ebd92c39827a5a16"},
   "makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"},
   "makeup_elixir": {:hex, :makeup_elixir, "0.15.0", "98312c9f0d3730fde4049985a1105da5155bfe5c11e47bdc7406d88e01e4219b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "75ffa34ab1056b7e24844c90bfc62aaf6f3a37a15faa76b07bc5eba27e4a8b4a"},


Test patch:
diff --git a/test/phoenix_html/form_test.exs b/test/phoenix_html/form_test.exs
index c34188e..65d5a32 100644
--- a/test/phoenix_html/form_test.exs
+++ b/test/phoenix_html/form_test.exs
@@ -718,6 +718,19 @@ defmodule Phoenix.HTML.FormTest do
              ~s(<input id="key" name="search[key][]" type="datetime-local" value="2017-09-21T20:21">)
   end
 
+  test "datetime_local_input/3 with %DateTime{}" do
+    assert safe_to_string(
+             datetime_local_input(
+               :search,
+               :key,
+               value: ~U[2021-05-13 04:20:20.836851Z],
+               id: "key",
+               name: "search[key][]"
+             )
+           ) ==
+             ~s(<input id="key" name="search[key][]" type="datetime-local" value="2021-05-13T04:20">)
+  end
+
   test "datetime_local_input/3 with form" do
     assert safe_form(&datetime_local_input(&1, :key)) ==
              ~s(<input id="search_key" name="search[key]" type="datetime-local" value="value">)