<commit_before>from datetime import datetime 
from django.utils.translation import ugettext as _
from django.utils.translation import ungettext

JUST_NOW = _("just now")
SECONDS_AGO = (_("%(seconds)d second ago"), _("%(seconds)d seconds ago"))
MINUTES_AGO = (_("%(minutes)d minute ago"), _("%(minutes)d minutes ago"))
HOURS_AGO = (_("%(hours)d hour ago"), _("%(hours)d hours ago"))
YESTERDAY_AT = _("yesterday at %(time)s")
OLDER_YEAR = (_("year"), _("years"))
OLDER_MONTH = (_("month"), _("months"))
OLDER_WEEK = (_("week"), _("weeks"))
OLDER_DAY = (_("day"), _("days"))
OLDER_CHUNKS = (
    (365.0, OLDER_YEAR),
    (30.0, OLDER_MONTH),
    (7.0, OLDER_WEEK),
    (1.0, OLDER_DAY),
)
OLDER_AGO = _("%(number)d %(type)s ago")


def _un(singular, plural, n=None):
    return ungettext(singular, plural, n)


def naturaldate(date):

    if not date:
        return

    now = datetime.now()
    today = datetime(now.year, now.month, now.day)
    delta = now - date
    delta_midnight = today - date

    days = delta.days
    hours = round(delta.seconds / 3600, 0)
    minutes = delta.seconds / 60
    seconds = delta.seconds

    if days < 0:
        return JUST_NOW

    if days == 0:
        if hours == 0:
            if minutes > 0:
                return _un(*MINUTES_AGO, n=minutes) % {"minutes": minutes}
            else:
                return JUST_NOW
        else:
            return _un(*HOURS_AGO, n=hours) % {"hours": hours}

    if delta_midnight.days == 0:
        return YESTERDAY_AT % {"time": date.strftime("%H:%M")}

    count = 0
    for chunk, singular_plural in OLDER_CHUNKS:
        if days >= chunk:
            count = round((delta_midnight.days + 1) / chunk, 0)
            type_ = _un(*singular_plural, n=count)
            break

    return OLDER_AGO % {"number": count, "type": type_}

<commit_msg>Move the portal_tags to djangolets_tags to avoid any kind of circular dependency and refactor all the app accordigly.<commit_after>from datetime import datetime 
from django.utils.translation import ugettext as _
from django.utils.translation import ungettext

JUST_NOW = _("just now")
SECONDS_AGO = (_("%(seconds)d second ago"), _("%(seconds)d seconds ago"))
MINUTES_AGO = (_("%(minutes)d minute ago"), _("%(minutes)d minutes ago"))
HOURS_AGO = (_("%(hours)d hour ago"), _("%(hours)d hours ago"))
YESTERDAY_AT = _("yesterday at %(time)s")
OLDER_YEAR = (_("year"), _("years"))
OLDER_MONTH = (_("month"), _("months"))
OLDER_WEEK = (_("week"), _("weeks"))
OLDER_DAY = (_("day"), _("days"))
OLDER_CHUNKS = (
    (365.0, OLDER_YEAR),
    (30.0, OLDER_MONTH),
    (7.0, OLDER_WEEK),
    (1.0, OLDER_DAY),
)
OLDER_AGO = _("%(number)d %(type)s ago")


def _un(singular, plural, n=None):
    return ungettext(singular, plural, n)


def naturaldate(date):

    if not date:
        # a simple return will return a None
        return ''

    now = datetime.now()
    today = datetime(now.year, now.month, now.day)
    delta = now - date
    delta_midnight = today - date

    days = delta.days
    hours = round(delta.seconds / 3600, 0)
    minutes = delta.seconds / 60
    seconds = delta.seconds

    if days < 0:
        return JUST_NOW

    if days == 0:
        if hours == 0:
            if minutes > 0:
                return _un(*MINUTES_AGO, n=minutes) % {"minutes": minutes}
            else:
                return JUST_NOW
        else:
            return _un(*HOURS_AGO, n=hours) % {"hours": hours}

    if delta_midnight.days == 0:
        return YESTERDAY_AT % {"time": date.strftime("%H:%M")}

    count = 0
    for chunk, singular_plural in OLDER_CHUNKS:
        if days >= chunk:
            count = round((delta_midnight.days + 1) / chunk, 0)
            type_ = _un(*singular_plural, n=count)
            break

    return OLDER_AGO % {"number": count, "type": type_}


{'fraction_non_alphanumeric': 0.14915966386554622, 'fraction_numerical': 0.01365546218487395, 'mean_word_length': 2.863083164300203, 'pattern_counts': {'":': 10, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5680618', 'n_tokens_mistral': 1417, 'n_tokens_neox': 1307, 'n_words': 350}