Skip to content

Commit d0fa299

Browse files
committed
Format datetimes
1 parent e2f28c6 commit d0fa299

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

lib/ex_rss_web/feed_live/index.ex

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ defmodule ExRssWeb.FeedLive.Index do
400400
<li class="flex flex-col md:flex-row">
401401
<div class="flex flex-col">
402402
<a href={@entry.url} target="_blank"><%= @entry.title %></a>
403-
<span><%= @entry.posted_at %></span>
403+
<span><%= format_updated_at(@entry.posted_at) %></span>
404404
</div>
405405
406406
<div class="md:shrink-0 flex self-start mt-1 ml-auto space-x-4">
@@ -453,4 +453,40 @@ defmodule ExRssWeb.FeedLive.Index do
453453
"""
454454
end
455455
end
456+
457+
def format_updated_at(updated_at, attrs \\ []) do
458+
duration =
459+
Timex.Interval.new(from: updated_at, until: Timex.now())
460+
|> Timex.Interval.duration(:duration)
461+
462+
duration_in_days = Timex.Duration.to_days(duration)
463+
464+
if duration_in_days > 5 do
465+
case Timex.format(
466+
updated_at,
467+
"%B %d, %Y, %k:%M",
468+
:strftime
469+
) do
470+
{:ok, formatted_updated_at} ->
471+
formatted_updated_at
472+
473+
_ ->
474+
{default, _attrs} = Keyword.pop(attrs, :default, "n/a")
475+
476+
default
477+
end
478+
else
479+
# There is also a relative formatter provided by Timex in case the code
480+
# below needs to be changed or improved.
481+
#
482+
# https://hexdocs.pm/timex/Timex.Format.DateTime.Formatters.Relative.html#summary
483+
formatted_duration =
484+
duration
485+
|> Timex.Duration.to_minutes(truncate: true)
486+
|> Timex.Duration.from_minutes()
487+
|> Timex.format_duration(:humanized)
488+
489+
"#{formatted_duration} ago"
490+
end
491+
end
456492
end

lib/ex_rss_web/feed_live/index.html.heex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</li>
2525
<li class="inline-block mr-2"><%= feed.unread_entries_count %> unread</li>
2626
<li class="inline-block mr-2">
27-
last successful update at <%= feed.last_successful_update_at %>
27+
last update <%= format_updated_at(feed.last_successful_update_at) %>
2828
</li>
2929
</ul>
3030
</div>

0 commit comments

Comments
 (0)