@@ -400,7 +400,7 @@ defmodule ExRssWeb.FeedLive.Index do
400
400
< li class = "flex flex-col md:flex-row " >
401
401
< div class = "flex flex-col " >
402
402
< a href = { @ entry . url } target = "_blank " > <%= @ entry . title %> </ a >
403
- < span > <%= @ entry . posted_at %> </ span >
403
+ < span > <%= format_updated_at ( @ entry . posted_at ) %> </ span >
404
404
</ div >
405
405
406
406
< div class = "md:shrink-0 flex self-start mt-1 ml-auto space-x-4 " >
@@ -453,4 +453,40 @@ defmodule ExRssWeb.FeedLive.Index do
453
453
"""
454
454
end
455
455
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
456
492
end
0 commit comments