date.ex 876 B

12345678910111213141516171819202122232425262728293031323334353637
  1. defmodule CvGenView.Date do
  2. use Phoenix.Component
  3. attr(:date, :any)
  4. attr(:rest, :global)
  5. def date(assigns) do
  6. ~H"""
  7. <time datetime={@date} {@rest}><%= localized(@date) %></time>
  8. """
  9. end
  10. defp localized(nil), do: "Aujourd'hui"
  11. defp localized(date) when is_binary(date) do
  12. case String.split(date, "-") do
  13. [year, month] ->
  14. {year, month} = {String.to_integer(year), String.to_integer(month)}
  15. Date.new!(year, month, 1) |> localized()
  16. [year] ->
  17. year
  18. end
  19. end
  20. defp localized(date) when is_struct(date, Date),
  21. do:
  22. Calendar.strftime(
  23. date,
  24. "%B %Y",
  25. month_names: fn month ->
  26. {"Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre",
  27. "Octobre", "Novembre", "Décembre"}
  28. |> elem(month - 1)
  29. end
  30. )
  31. end