Remove timex

This commit is contained in:
Mirai Kumiko 2025-06-17 12:56:46 +02:00
parent 10756907dc
commit f43b4bd038
Signed by: miraikumiko
GPG key ID: 3F178B1B5E0CB278
3 changed files with 60 additions and 47 deletions

View file

@ -1,77 +1,92 @@
defmodule NullaWeb.UserHTML do defmodule NullaWeb.ActorHTML do
use NullaWeb, :html use NullaWeb, :html
embed_templates "templates/user/*" embed_templates "templates/actor/*"
def format_birthdate(date) do def format_birthdate(date) do
formatted = Date.to_string(date) |> String.replace("-", "/") formatted = Date.to_string(date) |> String.replace("-", "/")
age = Timex.diff(Timex.today(), date, :years)
today = Date.utc_today()
age =
today.year - date.year -
if {today.month, today.day} < {date.month, date.day}, do: 1, else: 0
"#{formatted} (#{age} years old)" "#{formatted} (#{age} years old)"
end end
def format_registration_date(date) do def format_registration_date(date) do
now = Timex.now() now = Date.utc_today()
formatted = Date.to_string(date) |> String.replace("-", "/") formatted = Date.to_string(date) |> String.replace("-", "/")
diff = Timex.diff(now, date, :days) diff_days = Date.diff(now, date)
relative = cond do
cond do diff_days == 0 ->
diff == 0 -> "#{formatted} (today)"
"today"
diff_days == 1 ->
diff == 1 -> "#{formatted} (1 day ago)"
"1 day ago"
diff_days < 30 ->
diff < 30 -> "#{formatted} (#{diff_days} days ago)"
"#{diff} days ago"
diff_days < 365 ->
diff < 365 -> year_diff = now.year - date.year
months = Timex.diff(now, date, :months) month_diff = now.month - date.month
if months == 1, do: "1 month ago", else: "#{months} months ago" day_correction = if now.day < date.day, do: -1, else: 0
months = year_diff * 12 + month_diff + day_correction
true -> if months == 1 do
years = Timex.diff(now, date, :years) "#{formatted} (1 month ago)"
if years == 1, do: "1 year ago", else: "#{years} years ago" else
end "#{formatted} (#{months} months ago)"
end
"#{formatted} (#{relative})"
true ->
year_diff = now.year - date.year
years = if {now.month, now.day} < {date.month, date.day}, do: year_diff - 1, else: year_diff
if years == 1 do
"#{formatted} (1 year ago)"
else
"#{formatted} (#{years} years ago)"
end
end
end end
def format_note_datetime(datetime) do def format_note_datetime(datetime) do
Timex.format!(datetime, "{0D} {Mfull} {YYYY}, {h24}:{m}", :strftime) Calendar.strftime(datetime, "%d %B %Y, %H:%M")
end end
def format_note_datetime_diff(datetime) do def format_note_datetime_diff(datetime) do
now = Timex.now() now = DateTime.utc_now()
diff = Timex.diff(now, datetime, :seconds) diff_seconds = DateTime.diff(now, datetime)
cond do cond do
diff < 60 -> diff_seconds < 60 ->
"now" "now"
diff < 3600 -> diff_seconds < 3600 ->
minutes = div(diff, 60) minutes = div(diff_seconds, 60)
"#{minutes}m ago" "#{minutes}m ago"
diff < 86400 -> diff_seconds < 86400 ->
hours = div(diff, 3600) hours = div(diff_seconds, 3600)
"#{hours}h ago" "#{hours}h ago"
diff < 518_400 -> diff_seconds < 604_800 ->
days = div(diff, 86400) days = div(diff_seconds, 86400)
"#{days}d ago" "#{days}d ago"
diff < 2_419_200 -> diff_seconds < 2_592_000 ->
weeks = div(diff, 604_800) weeks = div(diff_seconds, 604_800)
"#{weeks}w ago" "#{weeks}w ago"
diff < 28_512_000 -> diff_seconds < 31_536_000 ->
months = Timex.diff(now, datetime, :months) months = div(diff_seconds, 2_592_000)
"#{months}mo ago" "#{months}mo ago"
true -> true ->
years = Timex.diff(now, datetime, :years) years = div(diff_seconds, 31_536_000)
"#{years}y ago" "#{years}y ago"
end end
end end

View file

@ -24,13 +24,12 @@ defmodule NullaWeb.ActorController do
|> send_resp(200, Jason.encode!(ActivityPub.actor(actor))) |> send_resp(200, Jason.encode!(ActivityPub.actor(actor)))
else else
notes = Note.get_latest_notes(actor.id) notes = Note.get_latest_notes(actor.id)
following = Utils.count_following_by_username!(actor.preferredUsername) following = 0
followers = Utils.count_followers_by_username!(actor.preferredUsername) followers = 0
render( render(
conn, conn,
:show, :show,
domain: domain,
actor: actor, actor: actor,
notes: notes, notes: notes,
following: following, following: following,

View file

@ -43,7 +43,6 @@ defmodule Nulla.MixProject do
{:phoenix_live_dashboard, "~> 0.8.3"}, {:phoenix_live_dashboard, "~> 0.8.3"},
{:esbuild, "~> 0.8", runtime: Mix.env() == :dev}, {:esbuild, "~> 0.8", runtime: Mix.env() == :dev},
{:tailwind, "~> 0.2.0", runtime: Mix.env() == :dev}, {:tailwind, "~> 0.2.0", runtime: Mix.env() == :dev},
{:timex, "~> 3.7"},
{:heroicons, {:heroicons,
github: "tailwindlabs/heroicons", github: "tailwindlabs/heroicons",
tag: "v2.1.1", tag: "v2.1.1",