lib/core/formatter.rb
# Given a DateTime instance, which is assumed to refer to some time in
# the past, produce a human-readable represention for how long ago this
# time was. To be used for creation and modification timestamps.
Given a DateTime instance, which is assumed to refer to some time in
the past, produce a human-readable represention for how long ago this
time was. To be used for creation and modification timestamps.
Given a DateTime instance which is assumed to refer to some time in the past produce a human - readable represention for how long ago this time was . To be used for creation and modification timestamps .
def format_modification_time(mtime)
        unless mtime.is_a?(DateTime)
          raise ArgumentError, "format_modification_time: expected argument of class DateTime, got #{mtime.class.to_s}"
        end
        now = DateTime.now
        diff_in_seconds = ((now - mtime) * 24 * 60 * 60).to_i
        if diff_in_seconds < 0
          text = 'soon'
        elsif diff_in_seconds < 60
          text = 'just now'
        elsif diff_in_seconds < 60 * 60
          diff_in_minutes = (diff_in_seconds / 60).to_i
          text = diff_in_minutes == 1 ? '1 minute ago' : "#{diff_in_minutes} minutes ago"
        elsif diff_in_seconds < 60 * 60 * 24
          diff_in_hours = (diff_in_seconds / 60 / 60).to_i
          text = diff_in_hours == 1 ? '1 hour ago' : "#{diff_in_hours} hours ago"
        else
          diff_in_full_days = (now.to_date - mtime.to_date).to_i
          text = diff_in_full_days == 1 ? 'yesterday' : "#{diff_in_full_days} days ago"
        end
        return ActionController::Base.helpers.content_tag(:span, title: mtime.strftime("%a %F %T %:z")) { text }
      end
def format_modification_time ( mtime ) unless mtime . is_a? ( DateTime ) raise ArgumentError , "format_modification_time: expected argument of class DateTime, got #{mtime.class.to_s}" end now = DateTime . now diff_in_seconds = ( ( now - mtime ) * 24 * 60 * 60 ) . to_i if diff_in_seconds < 0 text = 'soon' elsif diff_in_seconds < 60 text = 'just now' elsif diff_in_seconds < 60 * 60 diff_in_minutes = ( diff_in_seconds / 60 ) . to_i text = diff_in_minutes == 1 ? '1 minute ago' : "#{diff_in_minutes} minutes ago" elsif diff_in_seconds < 60 * 60 * 24 diff_in_hours = ( diff_in_seconds / 60 / 60 ) . to_i text = diff_in_hours == 1 ? '1 hour ago' : "#{diff_in_hours} hours ago" else diff_in_full_days = ( now . to_date - mtime . to_date ) . to_i text = diff_in_full_days == 1 ? 'yesterday' : "#{diff_in_full_days} days ago" end return ActionController :: Base . helpers . content_tag ( :span , title : mtime . strftime ( "%a %F %T %:z" ) ) { text } end
Given a DateTime instance, which is assumed to refer to some time in
the past, produce a human-readable represention for how long ago this
time was.
Given a DateTime instance which is assumed to refer to some time in the past produce a human - readable represention for how long ago this time was .
# check for exact class (can't rely on duck-typing here since different # datetime classes behave subtly different; e.g. subtraction of # DateTime gives a diff in days, but subtraction of # ActiveSupport::TimeWithZone gives a diff in seconds) # the minus operator returns the difference in days; convert to seconds # please check your NTP client :) # count actual calendar days # show exact mtime in tooltip (like Github does)
mtime
<not_specific>