Replace invalid/unsupported dates with with epoch
Some checks are pending
ci/woodpecker/pr/woodpecker Pipeline is pending

Masto API requires “ISO 8601 Datetime”, not “ISO 8601 Datetime or empty”
This commit is contained in:
Charlotte 🦝 Delenk 2023-01-08 09:15:23 +01:00
parent 2ce4f96f6e
commit 8d55b8c6d9
Signed by untrusted user: darkkirb
GPG key ID: AB2BD8DAF2E37122
2 changed files with 9 additions and 9 deletions

View file

@ -332,7 +332,7 @@ def to_masto_date(%NaiveDateTime{} = date) do
# It supports negative years for example. # It supports negative years for example.
# ISO8601 only supports years before 1583 with mutual agreement # ISO8601 only supports years before 1583 with mutual agreement
if date.year < 1583 do if date.year < 1583 do
"" "1970-01-01T00:00:00Z"
else else
date date
|> NaiveDateTime.to_iso8601() |> NaiveDateTime.to_iso8601()
@ -344,11 +344,11 @@ def to_masto_date(date) when is_binary(date) do
with {:ok, date} <- NaiveDateTime.from_iso8601(date) do with {:ok, date} <- NaiveDateTime.from_iso8601(date) do
to_masto_date(date) to_masto_date(date)
else else
_ -> "" _ -> "1970-01-01T00:00:00Z"
end end
end end
def to_masto_date(_), do: "" def to_masto_date(_), do: "1970-01-01T00:00:00Z"
defp shortname(name) do defp shortname(name) do
with max_length when max_length > 0 <- with max_length when max_length > 0 <-

View file

@ -495,16 +495,16 @@ test "removes microseconds from date (String)" do
assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z" assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z"
end end
test "returns empty string when date invalid" do test "returns unix epoch when date invalid" do
assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == "" assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == "1970-01-01T00:00:00Z"
end end
test "returns empty string when date is before the introduction of the Gregorian Calendar" do test "returns unix epoch when date is before the introduction of the Gregorian Calendar" do
assert Utils.to_masto_date("0621-01-01T00:00:00Z") == "" assert Utils.to_masto_date("0621-01-01T00:00:00Z") == "1970-01-01T00:00:00Z"
end end
test "returns empty string when date is BCE" do test "returns unix epoch when date is BCE" do
assert Utils.to_masto_date("-420-01-01T00:00:00Z") == "" assert Utils.to_masto_date("-0420-01-01T00:00:00Z") == "1970-01-01T00:00:00Z"
end end
end end