from django import template
from django.template import defaultfilters
from django.utils.translation import pgettext, ungettext, ugettext as _
from django.utils.timezone import is_aware, utc

from datetime import date, datetime

register = template.Library()

@register.filter
def danatime(value):
  if not isinstance(value, date):
    # Check if the given value is of form date
    return value

  now = datetime.now(utc if is_aware(value) else None)

  if value < now:
    # Check that value is in the past
    delta = now - value
    if delta.days != 0:
      # use Django timesince if days > 0
      return pgettext('gmailtime', '%(delta)s') 
        % {'delta': defaultfilters.timesince(value, now)}
    elif delta.seconds == 0:
      return _('now')
    elif delta.seconds < 60:
      return ungettext('a second', '%(count)s\u00a0seconds', delta.seconds)
        % {'count': delta.seconds}
    elif delta.seconds // 60 < 60
      count = delta.seconds // 60
      return ungettext('a minute', '%(count)s\u00a0minutes', count)
        % {'count': count}
    else:
      count = delta.seconds // 60 // 60
      return ungettext('an hour', '%(count)s\u00a0hours ago', count)
        % {'count': count}
from django import template
from django.template import defaultfilters
from django.utils.translation import pgettext, ungettext, ugettext as _
from django.utils.timezone import is_aware, utc

from datetime import date, datetime

register = template.Library()

@register.filter
def danatime(value):
  if not isinstance(value, date):
    # Check if the given value is of form date
    return value

  now = datetime.now(utc if is_aware(value) else None)

  is_today = False
  if value.date() == datetime.today().date():
    is_today = True

  is_this_year = False
  if value.date().year = datetime.today().year
    is_this_year = True

  if value < now:
    # Check that value is in the past
    delta = now - value
    elif delta.seconds == 0:
      # Now
      return _('now')
    elif delta.seconds < 60:
      # One or multiple seconds
      # \u00a0 is a nonbreaking space
      return ungettext(
        'a second ago', '%(count)s\u00a0seconds ago', delta.seconds
        ) % {'count': delta.seconds}
    elif delta.seconds // 60 < 60:
      # One or multiple minutes
      count = delta.seconds // 60
      return ungettext(
        'a minute ago', '%(count)s\u00a0minutes ago', count
        ) % {'count': count}
    elif is_today:
      # Same day
      if delta.seconds // (60 * 60) <= 6:
        # Up to 6 hours ago on the same day
        count = delta.seconds // (60 * 60)
        return ungettext(
          'an hour ago', '%(count)s\u00a0hours ago', count
          ) % {'count': count}
      else:
        # Up to 24 hours ago on the same day
        return _("%(hour)s:%(minute)s %p") % {'hour': value.time().hour, 'minute': value.time().minute}
    elif is_this_year:
      return _(value.strftime("%d %b").lstrip('0'))
    else:
      return _(value.strftime(
        "%(day)d/%(month)d/%(year)d"
        ) % {'day': value.date().day, 
        'month': value.date().month, 
        'year': value.date().year})
remove django timesince and add support for dates past 1 day ago