Repository: phoenixframework/phoenix
Pull request #5107 (2022-11-13 21:41:40)

Solves date, and time issues for phx generator  #5077 and #5098.
This PR solves the following issues #5077 and #5098.

<img width="702" alt="Screenshot 2022-11-13 at 14 02 23" src="https://user-images.githubusercontent.com/5170054/201545931-0f70101a-ab15-4210-8bad-dac327f5f1ac.png">



Patch:
diff --git a/installer/templates/phx_web/components/core_components.ex b/installer/templates/phx_web/components/core_components.ex
index c7ee92386..6cb79fc79 100644
--- a/installer/templates/phx_web/components/core_components.ex
+++ b/installer/templates/phx_web/components/core_components.ex
@@ -271,7 +271,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
       if assigns.multiple, do: name <> "[]", else: name
     end)
     |> assign_new(:id, fn -> Phoenix.HTML.Form.input_id(f, field) end)
-    |> assign_new(:value, fn -> Phoenix.HTML.Form.input_value(f, field) end)
+    |> assign_new(:value, fn -> maybe_format_input_value(Phoenix.HTML.Form.input_value(f, field)) end)
     |> assign_new(:errors, fn -> translate_errors(f.errors || [], field) end)
     |> input()
   end
@@ -634,4 +634,12 @@ defmodule <%= @web_namespace %>.CoreComponents do
   defp input_equals?(val1, val2) do
     Phoenix.HTML.html_escape(val1) == Phoenix.HTML.html_escape(val2)
   end
+
+  defp maybe_format_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
+
+  defp maybe_format_input_value(other), do: other
 end
diff --git a/lib/mix/phoenix/schema.ex b/lib/mix/phoenix/schema.ex
index 4777a31bf..f541b49bc 100644
--- a/lib/mix/phoenix/schema.ex
+++ b/lib/mix/phoenix/schema.ex
@@ -206,9 +206,9 @@ defmodule Mix.Phoenix.Schema do
   Since `form_component.html.heex` generated by the live generator uses selects for dates and/or
   times, fixtures must use map format for those fields in order to submit the live form.
   """
-  def live_form_value(%Date{} = date), do: "#{date.year}-#{date.month}-#{date.day}"
+  def live_form_value(%Date{} = date), do: Calendar.strftime(date, "%Y-%m-%d")
 
-  def live_form_value(%Time{} = time), do: "#{time.hour}:#{time.minute}"
+  def live_form_value(%Time{} = time), do: Calendar.strftime(time, "%H:%M")
 
   def live_form_value(%NaiveDateTime{} = naive) do
     NaiveDateTime.to_iso8601(naive)
diff --git a/priv/templates/phx.gen.live/core_components.ex b/priv/templates/phx.gen.live/core_components.ex
index c7ee92386..6cb79fc79 100644
--- a/priv/templates/phx.gen.live/core_components.ex
+++ b/priv/templates/phx.gen.live/core_components.ex
@@ -271,7 +271,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
       if assigns.multiple, do: name <> "[]", else: name
     end)
     |> assign_new(:id, fn -> Phoenix.HTML.Form.input_id(f, field) end)
-    |> assign_new(:value, fn -> Phoenix.HTML.Form.input_value(f, field) end)
+    |> assign_new(:value, fn -> maybe_format_input_value(Phoenix.HTML.Form.input_value(f, field)) end)
     |> assign_new(:errors, fn -> translate_errors(f.errors || [], field) end)
     |> input()
   end
@@ -634,4 +634,12 @@ defmodule <%= @web_namespace %>.CoreComponents do
   defp input_equals?(val1, val2) do
     Phoenix.HTML.html_escape(val1) == Phoenix.HTML.html_escape(val2)
   end
+
+  defp maybe_format_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
+
+  defp maybe_format_input_value(other), do: other
 end


Test patch:
diff --git a/test/mix/phoenix_test.exs b/test/mix/phoenix_test.exs
index 2fde37a23..2bcd1da05 100644
--- a/test/mix/phoenix_test.exs
+++ b/test/mix/phoenix_test.exs
@@ -102,8 +102,10 @@ defmodule Mix.PhoenixTest do
       )
 
   test "live_form_value/1" do
-    assert Mix.Phoenix.Schema.live_form_value(~D[2020-10-09]) == "2020-10-9"
-    assert Mix.Phoenix.Schema.live_form_value(~T[14:00:00]) == "14:0"
+    assert Mix.Phoenix.Schema.live_form_value(~D[2020-10-09]) == "2020-10-09"
+    assert Mix.Phoenix.Schema.live_form_value(~T[14:00:00]) == "14:00"
+    assert Mix.Phoenix.Schema.live_form_value(~T[14:01:00]) == "14:01"
+    assert Mix.Phoenix.Schema.live_form_value(~T[14:15:40]) == "14:15"
 
     assert Mix.Phoenix.Schema.live_form_value(~N[2020-10-09 14:00:00]) == "2020-10-09T14:00:00"