78 lines
1.7 KiB
Elixir
78 lines
1.7 KiB
Elixir
defmodule NullaWeb.UserHTML do
|
|
use NullaWeb, :html
|
|
|
|
embed_templates "templates/user/*"
|
|
|
|
def format_birthdate(date) do
|
|
formatted = Date.to_string(date) |> String.replace("-", "/")
|
|
age = Timex.diff(Timex.today(), date, :years)
|
|
"#{formatted} (#{age} years old)"
|
|
end
|
|
|
|
def format_registration_date(date) do
|
|
now = Timex.now()
|
|
formatted = Date.to_string(date) |> String.replace("-", "/")
|
|
|
|
diff = Timex.diff(now, date, :days)
|
|
|
|
relative =
|
|
cond do
|
|
diff == 0 ->
|
|
"today"
|
|
|
|
diff == 1 ->
|
|
"1 day ago"
|
|
|
|
diff < 30 ->
|
|
"#{diff} days ago"
|
|
|
|
diff < 365 ->
|
|
months = Timex.diff(now, date, :months)
|
|
if months == 1, do: "1 month ago", else: "#{months} months ago"
|
|
|
|
true ->
|
|
years = Timex.diff(now, date, :years)
|
|
if years == 1, do: "1 year ago", else: "#{years} years ago"
|
|
end
|
|
|
|
"#{formatted} (#{relative})"
|
|
end
|
|
|
|
def format_note_datetime(datetime) do
|
|
Timex.format!(datetime, "{0D} {Mfull} {YYYY}, {h24}:{m}", :strftime)
|
|
end
|
|
|
|
def format_note_datetime_diff(datetime) do
|
|
now = Timex.now()
|
|
diff = Timex.diff(now, datetime, :seconds)
|
|
|
|
cond do
|
|
diff < 60 ->
|
|
"now"
|
|
|
|
diff < 3600 ->
|
|
minutes = div(diff, 60)
|
|
"#{minutes}m ago"
|
|
|
|
diff < 86400 ->
|
|
hours = div(diff, 3600)
|
|
"#{hours}h ago"
|
|
|
|
diff < 518_400 ->
|
|
days = div(diff, 86400)
|
|
"#{days}d ago"
|
|
|
|
diff < 2_419_200 ->
|
|
weeks = div(diff, 604_800)
|
|
"#{weeks}w ago"
|
|
|
|
diff < 28_512_000 ->
|
|
months = Timex.diff(now, datetime, :months)
|
|
"#{months}mo ago"
|
|
|
|
true ->
|
|
years = Timex.diff(now, datetime, :years)
|
|
"#{years}y ago"
|
|
end
|
|
end
|
|
end
|