Skip to content

Commit 755c06a

Browse files
authored
fix(front: refactor timestamps format to up to 1 hour (#168)
1 parent 62450a8 commit 755c06a

File tree

3 files changed

+53
-17
lines changed

3 files changed

+53
-17
lines changed

front/assets/js/time_ago.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,20 @@ class TimeAgo extends HTMLElement {
3333
decorateRelative(date) {
3434
const now = new Date();
3535
const diffInSeconds = Math.floor((now - date) / 1000);
36+
const diffInHours = Math.floor(diffInSeconds / 3600);
3637
const daysDifference = Math.floor(diffInSeconds / 86400);
3738

38-
return daysDifference > 3 ? this.formatFullDate(date) : this.formatRelativeTime(diffInSeconds);
39+
if (daysDifference > 3) return this.formatFullDate(date);
40+
if (diffInHours >= 1) return this.formatDateWithTime(date);
41+
return this.formatRelativeTime(diffInSeconds);
3942
}
4043

4144
formatRelativeTime(seconds) {
4245
const rtf = new Intl.RelativeTimeFormat(this.locale, { numeric: "auto" });
4346

4447
if (Math.abs(seconds) < 60) return rtf.format(-seconds, "second");
4548
if (Math.abs(seconds) < 3600) return rtf.format(-Math.floor(seconds / 60), "minute");
46-
if (Math.abs(seconds) < 86400) return rtf.format(-Math.floor(seconds / 3600), "hour");
47-
return rtf.format(-Math.floor(seconds / 86400), "day");
49+
return rtf.format(-Math.floor(seconds / 3600), "hour");
4850
}
4951

5052
formatFullDate(date) {
@@ -57,6 +59,17 @@ class TimeAgo extends HTMLElement {
5759
return `on ${weekday} ${suffixedDay} ${month} ${year}`;
5860
}
5961

62+
formatDateWithTime(date) {
63+
const options = { weekday: "short", day: "numeric", month: "short", year: "numeric", hour: "2-digit", minute: "2-digit", hour12: false };
64+
const formattedDate = date.toLocaleDateString(this.locale, options);
65+
const time = date.toLocaleTimeString(this.locale, { hour: "2-digit", minute: "2-digit", hour12: false });
66+
67+
const [weekday, month, day, year] = formattedDate.replaceAll(",", "").split(" ");
68+
const suffixedDay = day + this.ordinalSuffix(parseInt(day, 10));
69+
70+
return `${weekday} ${suffixedDay} ${month} ${year} at ${time}`;
71+
}
72+
6073
ordinalSuffix(day) {
6174
if ([11, 12, 13].includes(day)) return "th";
6275
switch (day % 10) {

front/lib/front/utils.ex

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,26 @@ defmodule Front.Utils do
297297
|> decorate_relative()
298298
end
299299

300-
def decorate_relative(date) do
301-
if Timex.diff(DateTime.utc_now(), date, :days) > 3 do
302-
unssufixed_date = Timex.format!(date, "%a %d %b %Y", :strftime)
303-
[weekday, day, month, year] = String.split(unssufixed_date, " ")
304-
305-
suffixed_day = day <> ordinal_suffix(String.to_integer(day))
306-
"on #{weekday} #{suffixed_day} #{month} #{year}"
307-
else
308-
Timex.format!(date, "{relative}", :relative)
300+
def decorate_relative(date), do: decorate_relative(DateTime.utc_now(), date)
301+
302+
def decorate_relative(from, until) do
303+
diff_days = Timex.diff(from, until, :days)
304+
diff_hours = Timex.diff(from, until, :hours)
305+
306+
unssufixed_date = Timex.format!(until, "%a %d %b %Y", :strftime)
307+
[weekday, day, month, year] = String.split(unssufixed_date, " ")
308+
suffixed_day = day <> ordinal_suffix(String.to_integer(day))
309+
310+
cond do
311+
diff_days > 3 ->
312+
"on #{weekday} #{suffixed_day} #{month} #{year}"
313+
314+
diff_hours >= 1 and diff_days <= 3 ->
315+
time_part = Timex.format!(until, "%H:%M", :strftime)
316+
"on #{weekday} #{suffixed_day} #{month} #{year} at #{time_part}"
317+
318+
true ->
319+
Timex.format!(until, "{relative}", :relative)
309320
end
310321
end
311322

front/test/front/utils_test.exs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,25 @@ defmodule Front.UtilsTest do
6464
end
6565

6666
test "decorate relative" do
67-
two_days_ago = Timex.now() |> Timex.shift(days: -2) |> Timex.to_unix()
68-
three_days_ago = Timex.now() |> Timex.shift(days: -3) |> Timex.to_unix()
67+
now = DateTime.utc_now()
68+
69+
thirty_minutes_ago = DateTime.add(now, -30 * 60, :second)
70+
71+
fake_today = ~U[2025-03-20 12:00:00.000000Z]
72+
two_days_ago = DateTime.new!(Date.add(fake_today, -2), ~T[14:30:00])
73+
three_days_ago = DateTime.new!(Date.add(fake_today, -3), ~T[10:15:00])
74+
6975
assert Front.Utils.decorate_relative(0) == ""
7076
assert Front.Utils.decorate_relative(nil) == ""
71-
assert Front.Utils.decorate_relative(DateTime.utc_now()) == "now"
72-
assert Front.Utils.decorate_relative(two_days_ago) == "2 days ago"
73-
assert Front.Utils.decorate_relative(three_days_ago) == "3 days ago"
77+
assert Front.Utils.decorate_relative(now) == "now"
78+
79+
assert Front.Utils.decorate_relative(thirty_minutes_ago) == "30 minutes ago"
80+
81+
assert Front.Utils.decorate_relative(fake_today, two_days_ago) ==
82+
"on Tue 18th Mar 2025 at 14:30"
83+
84+
assert Front.Utils.decorate_relative(fake_today, three_days_ago) ==
85+
"on Mon 17th Mar 2025 at 10:15"
7486

7587
assert Front.Utils.decorate_relative(~U[2025-03-05 22:05:26.833945Z]) ==
7688
"on Wed 05th Mar 2025"

0 commit comments

Comments
 (0)