logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

date_time.ex (868B)


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