require "format_time/version" module FormatTime include ActionView::Helpers::DateHelper class Error < StandardError; end # Your code goes here... def format_relative_time(time) return '-' if time.nil? distance_of_time_in_words_to_now(time, include_seconds: true) end def format_absolute_time(time) return '-' if time.nil? Time.current.year == time.year ? in_year(time) : previous_year(time) end private def in_year(time) if time.today? format_relative_time(time) elsif time.to_date == Date.yesterday I18n.l(time, format: :yesterday) elsif time.to_date.cweek == Date.current.cweek I18n.l(time, format: :in_week) else I18n.l(time, format: :normal) end end def previous_year(time) I18n.l(time, format: :previous_year) end end