logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

date_time.ex (1026B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.DateTime do
  5. @moduledoc """
  6. The AP standard defines the date fields in AP as xsd:DateTime. Elixir's
  7. DateTime can't parse this, but it can parse the related iso8601. This
  8. module punches the date until it looks like iso8601 and normalizes to
  9. it.
  10. DateTimes without a timezone offset are treated as UTC.
  11. Reference: https://www.w3.org/TR/activitystreams-vocabulary/#dfn-published
  12. """
  13. use Ecto.Type
  14. def type, do: :string
  15. def cast(datetime) when is_binary(datetime) do
  16. with {:ok, datetime, _} <- DateTime.from_iso8601(datetime) do
  17. {:ok, DateTime.to_iso8601(datetime)}
  18. else
  19. {:error, :missing_offset} -> cast("#{datetime}Z")
  20. _e -> :error
  21. end
  22. end
  23. def cast(_), do: :error
  24. def dump(data) do
  25. {:ok, data}
  26. end
  27. def load(data) do
  28. {:ok, data}
  29. end
  30. end