@contextfilter
def humanize_datetime(context, value):
    """
    Finds the difference between the datetime value given and now()
    and returns appropriate humanize form
    """

    request = context['request']

    user = None
    if request.user.username:
        try:
            user = request.user.profile
        except:
            pass

    # timezone
    default_timezone = settings.HARDTREE_SERVER_DEFAULT_TIMEZONE
    try:
        conf = ModuleSetting.get('default_timezone')[0]
        default_timezone = conf.value
    except:
        pass

    try:
        conf = ModuleSetting.get('default_timezone', user=user)[0]
        default_timezone = conf.value
    except Exception:
        default_timezone = getattr(
            settings, 'HARDTREE_SERVER_TIMEZONE')[default_timezone][0]

    all_timezones = getattr(settings, 'HARDTREE_SERVER_TIMEZONE', [
                            (1, '(GMT-11:00) International Date Line West')])
    title = all_timezones[int(default_timezone)][1]
    GMT = title[4:10]  # with sign e.g. +06:00
    sign = GMT[0:1]  # + or -
    hours = int(GMT[1:3])  # e.g. 06
    mins = int(GMT[4:6])

    now = datetime.now()

    if value:
        if sign == "-":
            value = value - timedelta(hours=hours, minutes=mins)
            now = now - timedelta(hours=hours, minutes=mins)
        else:
            value = value + timedelta(hours=hours, minutes=mins)
            now = now + timedelta(hours=hours, minutes=mins)

    if isinstance(value, timedelta):
        delta = value
    elif isinstance(value, datetime):
        delta = now - value
    else:
        delta = None

    if delta:
        if delta.days > 6:                                      # May 15, 17:55
            month = None
            if value.strftime("%b") == 'Jan':
                month = _("Jan")
            elif value.strftime("%b") == 'Feb':
                month = _("Feb")
            elif value.strftime("%b") == 'Mar':
                month = _("Mar")
            elif value.strftime("%b") == 'Apr':
                month = _("Apr")
            elif value.strftime("%b") == 'May':
                month = _("May")
            elif value.strftime("%b") == 'Jun':
                month = _("Jun")
            elif value.strftime("%b") == 'Jul':
                month = _("Jul")
            elif value.strftime("%b") == 'Aug':
                month = _("Aug")
            elif value.strftime("%b") == 'Sep':
                month = _("Sep")
            elif value.strftime("%b") == 'Oct':
                month = _("Oct")
            elif value.strftime("%b") == 'Nov':
                month = _("Nov")
            elif value.strftime("%b") == 'Dec':
                month = _("Dec")
            return month + value.strftime(" %d, %H:%M")

        if delta.days > 1:                                      # Wednesday
            if value.strftime("%A") == 'Monday':
                return _("Monday")
            elif value.strftime("%A") == 'Tuesday':
                return _("Tuesday")
            elif value.strftime("%A") == 'Wednesday':
                return _("Wednesday")
            elif value.strftime("%A") == 'Thursday':
                return _("Thursday")
            elif value.strftime("%A") == 'Friday':
                return _("Friday")
            elif value.strftime("%A") == 'Saturday':
                return _("Saturday")
            elif value.strftime("%A") == 'Sunday':
                return _("Sunday")

        elif delta.days == 1:
            return _("yesterday")                               # yesterday
        elif delta.seconds >= 7200:
            return str(delta.seconds / 3600) + _(" hours ago")  # 3 hours ago
        elif delta.seconds >= 3600:
            return _("1 hour ago")                              # 1 hour ago
        elif delta.seconds > MOMENT:
            # 29 minutes ago
            return str(delta.seconds / 60) + _(" minutes ago")
        else:
            return _("a moment ago")                            # a moment ago
        return djangodate(value)
    else:
        return str(value)
dataset/ETHPy150Open treeio/treeio/treeio/core/templatetags/modules.py/humanize_datetime/original